Step 11 of 12 · Binary search
Binary search index
Problem
Read a sorted list of distinct whole numbers and a target. Return the zero-based index of the target, or -1 when it is absent.
Input
The first line contains n. The second line contains n sorted integers. The third line contains the target.
Output
The target's zero-based index, or -1 when the target is absent.
Examples
6 -4 0 3 7 12 20 7
3
7 appears at zero-based index 3.
4 2 5 8 11 6
-1
6 does not appear in the sorted list.