First Occurrence in Sorted Array
Easyjavapythonccppjavascript
Given a sorted array that may contain duplicate values, find the first 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 first index of target, or -1 if it does not exist.
Example 1
Input
5 2 1 2 2 2 5
Output
1
Explanation: 2 first appears at index 1.
- 1 <= N <= 100000
- The array is sorted in non-decreasing order.
- -1000000000 <= A[i], target <= 1000000000