Binary Search
The door terminal at Riverbend Sports Club checks membership numbers against a register that is always held in ascending order. The club has tens of thousands of members and the door must answer while somebody is still pushing it, so reading the register from one end until the number turns up is not acceptable — the ordering is there to be used. Numbers that are not members must be reported as such rather than silently let through.
Task: Print the position of the membership number in the register, or report that it is absent.
Input
Two lines. The first holds between 1 and 1000 integers in ascending order with no repeats, separated by single spaces — the register. The second holds a single integer, the number to look up.
Output
One line: the position of that number in the register, counting from 0 for the first entry. Print -1 if the register does not contain it.
Example:
Input:
1 3 5 7 9 11
7
Output:
3
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in