You are given an array of integers sorted in non-decreasing order and a target value. The array may contain duplicates. Return the 0-based index of the first (leftmost) position at which the target appears. If the target does not appear anywhere in the array, return -1.
Line 1: an integer n, the length of the array.
Line 2: n space-separated integers in non-decreasing order (this line is empty when n = 0).
Line 3: an integer target.
A single integer: the index of the first occurrence of target, or -1 if it is not present.
Example 1
Input
7 -3 -1 -1 2 4 4 9 4
Expected
4
Explanation
The value 4 appears at indices 4 and 5; the first occurrence is index 4.
Example 2
Input
5 1 3 5 7 9 6
Expected
-1
Explanation
6 is not in the array, so the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →