Given a sorted array of unique integers and a target, return the index of the target if it exists, or -1 otherwise, in O(log n) time.
nums = [-1,0,3,5,9,12], target = 94nums = [-1,0,3,5,9,12], target = 2-1Try it now — no sign-up needed. Write your solution in Python or JavaScript, run it against test cases, and submit for a verdict, right in your browser.
Open the editor →Repeatedly halve the search space by comparing the middle element to a target, turning an O(n) scan into O(log n). It works on more than sorted arrays — any "answer space" that's monotonic (true…true…false…false) can be binary searched.
Read the full Binary Search guide →
Original problem on LeetCode ↗