ALG-E014EasyBinary Search
First Position in Sorted Data
In a non-decreasing array, return the first index containing target, or -1 if target is absent. Repeated values must return the earliest index.
Contract
input numbers: integer[]
input target: integer
return integer
Constraints
- • Input is sorted
- • 0 <= length <= 100000
Examples
Example 1
numbers = [1, 2, 2, 2, 5]
target = 2
→ 1
Example 2
numbers = [1, 4, 6]
target = 3
→ -1
5 judge tests (2 shown above). Target complexity: O(log n).