The pattern: binary search
Repeatedly halve a search space by testing the midpoint against a monotonic condition, turning linear scans into blazing O(log n) searches.
When data is sorted or a decision is monotonic, scanning one element at a time (O(n)) does far too much work. Binary search evaluates the middle element to eliminate an entire half of the remaining possibilities in a single comparison. In an array of one billion elements, a linear scan might take a billion checks; binary search finishes in at most 30 steps.
01The three shapes
1. Classic exact match on a closed range [lo, hi]. Both boundaries are inclusive. On each iteration, compare a[mid] directly against target. If equal, return mid. Otherwise, discard the half that cannot contain the target.
lo = 0 ; hi = n - 1
while lo <= hi:
mid = lo + (hi - lo) / 2
if a[mid] == target: return mid
if a[mid] < target: lo = mid + 1
else: hi = mid - 1
return -1Used for: Binary Search, Search in Rotated Sorted Array.
2. Boundary search (lower bound / first true). Find the first index where a condition becomes true (e.g., first value ≥ target, first bad version, or local slope change). The range is [lo, hi] and shrinks until lo == hi.
lo = 0 ; hi = n
while lo < hi:
mid = lo + (hi - lo) / 2
if condition(mid): hi = mid
else: lo = mid + 1
return loUsed for: Search Insert Position, First Bad Version, Find First and Last Position of Element in Sorted Array, Find Peak Element.
3. Binary search on the answer. Instead of searching an explicit array of indices, search across a range of possible answers [min_possible, max_possible]. A helper function tests whether a candidate answer mid is feasible. If it is, shrink the range toward more optimal values.
lo = min_answer ; hi = max_answer
while lo < hi:
mid = lo + (hi - lo) / 2
if feasible(mid): hi = mid
else: lo = mid + 1
return loUsed for: Arranging Coins, Split Array Largest Sum (and related interview classics like Koko Eating Bananas and Capacity To Ship Packages).
02How to spot it
- The input is sorted, or sorting it initially is inexpensive.
- The problem asks for O(log n) time complexity.
- The question asks for the "minimum that works", "maximum that fits", or "minimize the maximum".
- A monotonic condition exists: if a value
xworks, every value greater thanxalso works (or vice versa).
03The overflow trap: why lo + (hi - lo) / 2?
In Java, integers are signed 32-bit values with maximum value 2³¹ − 1 (about 2.14 billion). If lo and hi are large (for example, searching versions up to 2 billion), writing (lo + hi) / 2 adds two large numbers, overflows into negative numbers, and causes an infinite loop or array index crash.
Writing lo + (hi - lo) / 2 computes the exact same mathematical midpoint without the sum ever exceeding hi.
04Problems in this chapter
Binary Search, Search Insert Position, and First Bad Version are free to start with. Then: Arranging Coins, Find First and Last Position of Element in Sorted Array, Search in Rotated Sorted Array, Find Peak Element, and Split Array Largest Sum.