Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns

Topic · 2 patterns

Binary Search patterns

Binary search halves a search space whose answer flips exactly once. Plain binary search finds a position in sorted data; binary search on the answer guesses the answer itself and checks whether it is feasible, which works whenever "can it be done with x?" goes from no to yes at a single point.

The patterns

  1. Binary Search

    O(log n)

    Halve the search space each step; the trick in variants is deciding which half is sorted / which side to keep.

    Spot it: Sorted or rotated-sorted array, or a monotone yes/no predicate over indices: target, first/last position, insertion point, peak, O(log n).

    Learn Binary SearchCheat sheet and practice
  2. Binary Search on the Answer

    O(n log R)

    Binary-search the answer value itself; for each guess run a linear feasibility test that's monotonic in the guess.

    Spot it: "Minimize the maximum" / "maximize the minimum" / smallest X that's possible, over a large numeric range with an easy check for a guess.

    Learn first: Binary Search, Greedy

    Learn Binary Search on the AnswerCheat sheet and practice

Which one? Match the signal

If the problem saysReach for
Sorted or rotated-sorted array: find target, a boundary (first/last), or a peakBinary SearchO(log n)
"Minimize the maximum" / "maximize the minimum", or answer is testable with a yes/no checkBinary Search on AnswerO(n log R)

Easy to confuse

  • Split Array Largest Sum looks like Interval DP, but it’s Binary Search on the Answer

    DP over partitions works (O(k n2)), but 'minimise the largest' points to binary search with greedy check.

  • Median of Two Sorted Arrays looks like K-way Merge, but it’s Binary Search

    Merging is O(m + n); binary searching the partition is O(log min(m, n)).

  • Kth Smallest Element in a Sorted Matrix looks like K-way Merge, but it’s Binary Search on the Answer

    Heap works, but binary search on the value with a staircase count is often better.

  • Random Pick with Weight looks like Reservoir Sampling, but it’s Binary Search

    Weights known upfront: prefix sums + binary search, not reservoir sampling.

  • Find Peak Element looks like Two Pointers, but it’s Binary Search

    No sortedness, but the local slope gives a monotone predicate for binary search.

Used together

  • Binary Search on the Answer + Greedy

    When: minimise the maximum / maximise the minimum over splits, speeds, or capacities. Constructing the optimum directly is hard, but checking a candidate is a simple greedy pass.

  • Binary Search on the Answer + Graph BFS / DFS

    When: minimise the maximum height/effort along a path. Minimax path costs become reachability at a threshold.

  • Binary Search on the Answer + Two Pointers

    When: k-th smallest pair distance / k-th in a sorted matrix. Enumerating all pairs to find the k-th is O(n2); counting is O(n).

  • Longest Increasing Subsequence + Binary Search

    When: longest chain with n large. O(n2) LIS fails for n = 1e5.

Where to start

then the The 128 and The 365 sheets

Binary Search

  • Binary SearchEasy
  • Search Insert PositionEasy

Binary Search on the Answer

  • Koko Eating BananasMedium
  • Capacity To Ship Packages Within D DaysMedium

Further reading

free, outside Lessgo
  • Binary searchcp-algorithms

    Invariant-based binary search including search on answer; avoids off-by-one bugs.

  • Binary SearchUSACO Guide

    Binary search on monotonic predicates with graded problems.

  • ITMO Binary Search courseCodeforces EDU

    Video + problems; the best structured practice for search on answer.

  • Binary Search explore cardLeetCode

    Templates and rotated-array variants common in interviews.

  • Sorting and searching cheatsheetTech Interview Handbook

    Quick interview checklist and problem list for search problems.

← Linked ListsAll 49 patternsHeaps & Priority Queues →
TodayReviewPatternsProblems