Binary Search
Easy 20 XPSearchingArrays
Commonly asked at Google, Microsoft, Amazon
Read a sorted list of distinct integers and a target. Print the index of the target, or -1 if it is not present. Use binary search (O(log n)).
- Input
- Line 1: sorted integers. Line 2: target.
- Output
- Index or -1.
Sample 1
Input
-1 0 3 5 9 12 9
Output
4
Sample 2
Input
-1 0 3 5 9 12 2
Output
-1
Read input with input() (no prompt message) and print only the answer. Run tries the Input box; Submit checks all test cases.
Output Loading Python (first time takes a few seconds)
Press Run to see the result here.The solution unlocks after 3 submissions.