Binary Search
Practice this coding question on Coding Gurukul.
Problem statement
A sorted shelf contains distinct integer catalogue codes. Locate the requested code by repeatedly narrowing the search range. Report its zero-based position, or -1 when the code is absent. Your search should take logarithmic time.
Input format
First line: n target. Second line: n distinct integers in strictly increasing order.
Output format
Print the zero-based index, or -1.
Constraints
1 <= n <= 100000; -1000000000 <= codes, target <= 1000000000.
Sample input
5 14 -6 2 7 14 20
Sample output
3
Sign in to run or submit a solution in C++, Java or Python.