The Sacred Scroll Repository
View as PDFThe Sacred Scroll Repository
The Repository Keeper Thoth-Mes has arranged every scroll in strict numerical
order. When a scholar requests scroll number , Thoth-Mes uses a clever halving
strategy: check the middle, then discard the wrong half. Repeat until found or
declared lost.
Your Task
Given a sorted array of distinct integers and a target
, output the 0-based
index of
. If
is not found, output -1.
You must solve this using binary search.
Input Specification
The first line contains — the number of queries.
Each query consists of two lines:
Line 1: two integers
and
Line 2:
space-separated integers in sorted (ascending) order
Output Specification
For each query print one line: the 0-based index or -1.
Sample Input
3
7 14
2 5 8 12 14 23 38
5 10
1 3 5 7 9
4 25
10 20 30 40
Sample Output
4
-1
-1
Constraints
All values in each array are distinct.
Comments