← All coding problems
Binary Search
easyBinary Search+10 XPTime limit 2s
Given a sorted array of distinct integers and a target, print the index of target, or -1 if it is not present.
Input:
- line 1:
n - line 2:
ndistinct integers in increasing order - line 3:
target
Output: the 0-based index of target, or -1.
Constraints: 1 ≤ n ≤ 10⁵. Aim for O(log n).
Sample input 1
6 -1 0 3 5 9 12 9
Sample output 1
4
Sample input 2
6 -1 0 3 5 9 12 2
Sample output 2
-1
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).