Library Search (Arrays & Lists)

Read n sorted integers and q queries. For each query print its 0-based index, or -1 if absent. Values are distinct. Aim for binary search.

Input: n, the sorted numbers, q, then q queries.

Output: One index per query.

Difficulty: Medium. Write your answer in the browser editor, run the examples, then submit it against hidden tests. Python and JavaScript are free; other languages are part of licode Club.

Hint

  • Keep lo and hi, look at the middle, discard half each time.

More Arrays & Lists practice