Last Occurrence in Sorted Array
Easyjavapythonccppjavascript
Given a sorted array that may contain duplicate values, find the last index at which a target value appears.
Input Format
The first line contains two integers N and target. The second line contains N space-separated integers in non-decreasing order.
Output Format
Print the last index of target, or -1 if it does not exist.
Example 1
Input
5 2 1 2 2 2 5
Output
3
Explanation: The final 2 is at index 3.
- 1 <= N <= 100000
- The array is sorted in non-decreasing order.
- -1000000000 <= A[i], target <= 1000000000