Skip to content

Binary Search

The binary-search invariant

Binary search is deceptively simple: halve the search space using a comparison. The discipline comes from getting the invariants right, open vs. closed intervals, off-by-one at the boundaries, and correct loop termination. Once the basic template is solid, the pattern generalizes in three ways:

  • On sorted data: the textbook case (Binary Search, Search in 2D Matrix).
  • On rotated / partially-sorted data: use the fact that one half is always sorted (Find Minimum in Rotated, Search in Rotated).
  • On the answer space: when the value you’re solving for is monotonic in feasibility (Koko Eating Bananas, Capacity to Ship, Minimum Days to Make Bouquets).

The core question is always the same: can one comparison prove that half the search space cannot contain the answer? If yes, binary search is possible. If no, the array being sorted is not enough.

How to choose the variant

Prompt shapeVariantWhat the comparison proves
Sorted array, find exact targetClassic binary searchTarget is left, right, or found at mid.
Sorted matrix with row orderingFlattened index searchA virtual 1-D index maps back to (row, col).
Rotated sorted arrayModified binary searchAt least one half is sorted, so one side can be discarded.
Minimum feasible speed/capacity/dayBinary search on answerFeasibility changes from false to true at one boundary.
Time-stamped valuesPer-key binary searchTimestamps are sorted inside each key’s history.
Median across two sorted arraysPartition searchLeft partitions must contain the lower half of values.
Peak in a slope-shaped arraySlope-direction searchThe larger neighbor points toward at least one peak.

For answer-space problems, write the predicate first. It should read like can_finish(speed) or is_enough(capacity). Then prove it is monotonic: once a speed is fast enough, every larger speed is also fast enough. Without that one-way boundary, binary search on the answer is guessing.

Problems

  1. 704. Binary Search (Easy)
  2. 74. Search a 2D Matrix (Medium)
  3. 875. Koko Eating Bananas (Medium)
  4. 153. Find Minimum in Rotated Sorted Array (Medium)
  5. 33. Search in Rotated Sorted Array (Medium)
  6. 981. Time Based Key-Value Store (Medium)
  7. 4. Median of Two Sorted Arrays (Hard)

Bonus problems (same pattern, outside NeetCode 150):

Key patterns unlocked here

  • Canonical iterative binary search: 704.
  • Flattening a matrix to 1D: 74.
  • Binary search on the answer space: 875 (template for dozens of variations).
  • Detecting the sorted half: 153 and 33.
  • Per-key timeline binary search: 981 (bisect on timestamps).
  • Partition search on two arrays: 4 (the canonical hard binary-search problem).
  • Slope-direction binary search on unimodal arrays: 162.

Common mistakes

  • Moving both bounds past mid without proving the answer cannot be mid.
  • Mixing closed interval (left <= right) and half-open interval (left < right) templates in one solution.
  • Returning mid instead of the boundary variable in first-true or last-true searches.
  • Applying answer-space binary search before proving the predicate is monotonic.
  • Treating a rotated array as fully sorted. One half is sorted, not both.

How the problems fit together

Binary Search is the template page. Search a 2D Matrix shows that “sorted” can be virtual. Koko Eating Bananas is the answer-space template and is worth practicing until the predicate-first habit is automatic.

Find Minimum in Rotated Sorted Array and Search in Rotated Sorted Array teach partial order. Time Based Key-Value Store moves binary search inside a data structure. Median of Two Sorted Arrays is the hard capstone: the search target is not a value but a valid partition.