Skip to content

Binary Search Patterns

Master these 5 patterns and you can solve 95% of binary search problems on LeetCode.


#PatternTrigger PhraseKey Technique
1Classic Search”Find target in sorted array”lo <= hi, return mid
2First / Last Occurrence”Find leftmost / rightmost X”Don’t stop at match, keep shrinking
3Search on Answer”Minimum / maximum feasible value”Binary search on answer range, not array indices
4Rotated Sorted Array”Sorted but rotated at unknown pivot”Identify which half is sorted
5Monotonic Function”Find threshold where f(x) changes”lo < hi, converge to boundary

Binary Search Flowchart

What are you looking for?
│
├── Exact value in sorted array? ──────────→ Pattern 1 — Classic
├── First/last occurrence (duplicates)? ───→ Pattern 2 — Boundary
├── Min/max feasible value? ───────────────→ Pattern 3 — Answer Space
├── Rotated sorted array? ─────────────────→ Pattern 4 — Rotated
└── Monotonic/boolean threshold? ──────────→ Pattern 5 — Boundary

Problem SignalsPattern
”Find value in sorted array”Pattern 1 — Classic
”Find index of first/last X”Pattern 2 — First/Last
Array has duplicates, find rangePattern 2 — First/Last
”Minimum feasible X” / “Maximum X such that…”Pattern 3 — Answer Space
”Allocate minimum resources to satisfy constraint”Pattern 3 — Answer Space
Array is sorted but rotatedPattern 4 — Rotated Array
Function is monotonically increasing/decreasingPattern 5 — Monotonic Function
”First version / day / point where condition changes”Pattern 5 — Monotonic Function

StepFocusFile
1Classic Search — The foundation01-classic-search
2First/Last Occurrence — Handling duplicates02-first-last-occurrence
3Search on Answer — Thinking beyond arrays03-search-on-answer
4Rotated Array — Sorted but twisted04-rotated-array
5Monotonic Function — The boundary finder05-monotonic-function

Next: Pattern 1 — Classic Search →