Last index of element

 Problem Statement

Suggest Edit

Take an array with N elements with possibly duplicate elements as the input. The task is to find the index of the last occurrences of the element x in the array and, if it is not present, return -1.

Input Format:

The first line contains an integer N representing the size of the array.

The next line contains N space-separated integers representing the elements of the array.

The last line contains an integer 'x' whose index has to be found.

Output Format:

The only line of the output prints the Index or -1.
Constraints:
1 <= N <= 10^3
1 <= arr[i] <= 10^9
1 <= x < N
Sample Input 1 :
8
7 5 2 11 2 43 1 1
2
Sample Output 1 :
4
Explanation Of Sample Input 1:
2 is present twice in the input array and the last time it appears is at index 4.
Sample Input 2 :
8
7 5 2 11 2 43 1 1
10
Sample Output 2 :
-1
Explanation Of Sample Input 2:
10 is not present in the array so the output is -1.




#Your code goes here.

n=int(input())
lst=input().split()
lst=lst[::-1]
x=input()

try:
    print(n-int(lst.index(x))-1)
except ValueError:
    print(-1)























Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings