Last Index of Element

The task is to determine the index of the last occurrence of a specified element x within an array that may contain duplicate elements. If the element is not present, return -1.

Input:

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:

The only line of the output prints the Index or -1.

Example:

Input:
N = 5
arr = [2, 3, 4, 2, 5]
x = 2
Output:
3
Explanation:

The last occurrence of the element 2 is at the index 3 in the array.

Constraints:

  • 1 <= N <= 10^3
  • 1 <= arr[i] <= 10^9
  • 1 <= x < N
Note:

Consider 0-based indexing for the array.

Be the first one to answer
Add answer anonymously...
IEO Makers Fablab Web Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter