← Back to problems Solve on LeetCode → See #167 Two Sum II →

Binary Search

LeetCode 704 • Easy • Arrays

Input: nums = [-1, 0, 3, 5, 9, 12], target = 9  →  Output: 4
Find target in sorted array. Return index or -1 if not found.

TimeO(log n)halve each step
SpaceO(1)pointers only
Left: — Right: — Mid: —
Left
Right
Mid
Search space
Left
—
Right
—
Mid
—
Ready
Press Play. Binary search: left, right, mid. Halve search space each step. O(log n).