107. Binary Search
Core1000 ms256 MBSolved by 0%
You are given a sorted array of n integers and a value k.
Print the 0-based index where k appears, or -1 if it is not present. If k appears
more than once, print the index of any occurrence.
The array is sorted, so scanning from the start is not the intended solution.
Constraints - `1 ≤ n ≤ 1000000` - `-1000000000 ≤ a[i], k ≤ 1000000000` - The array is sorted ascending.
Input
The first line contains two integers n and k — the array length and the value to find.
The second line contains n space-separated integers.
Output
Print one integer — a 0-based index holding k, or -1 if it is absent.
Input5 3
1 3 5 7 9
Output1
Notefinds a value in the left half
Input5 100
1 3 5 7 9
Output-1
Notesearches for a value larger than everything present
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution