Binary Search problems, grouped by pattern

Binary search works on anything with a sorted or monotonic shape, not just sorted arrays. Once bisect_left and bisect_right are second nature, rotated arrays, 2D matrices and "minimum speed that works" problems all use the same loop.

When to reach for it

  • The input is sorted or rotated
  • Checking one candidate answer is easy, finding it is hard
  • The constraints hint at O(log n)

Mistakes to watch for

  • Infinite loops from mid rounding
  • Mixing inclusive and exclusive bounds
  • Overflow in (lo + hi) / 2 in Java

The 25 problems

1Basics

3Unique 1-D Binary Search

52D Binary Search + Step Search

7Binary Search on Answer Space