Skip to content
DSA Patterns
Menu
Language

Pattern #05

Binary Search on Answer

Essential

Binary search the answer value itself; check if a candidate is feasible in linear time.

When to use

You need min/max of some value X such that a greedy or scan can verify whether X is achievable. classic for capacity, speed, and split problems.

Recognition cues

  • Minimize the maximum / maximize the minimum
  • Koko eating bananas, ship packages, split array largest sum
  • Feasibility check is O(n) and monotonic in the guessed answer

Common pitfalls

  • Wrong search bounds (lo/hi too tight or too wide)
  • Feasibility that is not actually monotonic
  • Integer division when computing mid or rates

90-second recognition drill

Which pattern fits best?

  • Minimize the maximum / maximize the minimum
  • Koko eating bananas, ship packages, split array largest sum
  • Feasibility check is O(n) and monotonic in the guessed answer

Interactive

Mental model

A full worked walkthrough of the invariant. Pause, scrub the dots, or use ← →. Aim to narrate each step yourself.

Step 1 of 8
1/8

Search the answer domain, not array indices. Example: min feasible speed.

1F
2F
3F
4T
5T
6T
7T
8T
Statelo=1hi=8mid=4

feasible(x) is monotonic

How to think about it

You are not searching an array index. You search the answer domain [lo, hi]. The template finds the smallest feasible guess: while lo < hi, mid = lo + ((hi - lo) >> 1), and a true feasible(mid) sets hi = mid. A false guess sets lo = mid + 1. Every problem on this path is that minimum. Once a guess works, every larger guess stays feasible.

Template shapes

Shape Feasibility check Example
Split cap Count parts that stay within the cap. More than k parts means the cap is too small. Search from the largest element through the total LC 410
Gap A stretch needs ceil(distance / gap) - 1 new stations. More than k means the gap is too small LC 774
Speed Hours are the sum of ceil(pile / speed). More than h means too slow. The low end is 1, because speed 0 never finishes LC 875
Capacity Pack left to right and count days. More than days means too small. Capacity is at least the heaviest package LC 1011
Divisor A sum of ceil(num / divisor) above the threshold means the divisor is too small LC 1283
Day A bouquet is k adjacent flowers bloomed by that day. Fewer than m bouquets means too early. If m * k > n, the answer is -1 LC 1482

Complexity baseline

O(n log R), where R is the size of the answer range. Each probe is one linear scan.

From template to problem

  1. Prove the monotone direction. On this path, if a guess works, every larger guess works.
  2. Set tight bounds. A bound that excludes the true answer cannot be repaired by the loop.
  3. feasible only answers whether this guess is allowed. It does not move lo or hi.
  4. Keep the template’s first-true shrink. A feasible mid sets hi = mid. Raising lo to mid searches for a last true, which is a different problem.

Template

Same skeleton in TypeScript, Python, and C#. Adapt the invariant; keep the structure.

Binary Search on Answer · Template
/** Binary search on answer: first mid where feasible(mid) is true. */
export function binarySearchOnAnswer(
  lo: number,
  hi: number,
  feasible: (mid: number) => boolean,
): number {
  while (lo < hi) {
    const mid = lo + ((hi - lo) >> 1);
    if (feasible(mid)) hi = mid;
    else lo = mid + 1;
  }
  return lo;
}
/** Binary search on answer: first mid where feasible(mid) is true. */
export function binarySearchOnAnswer(
  lo: number,
  hi: number,
  feasible: (mid: number) => boolean,
): number {
  while (lo < hi) {
    const mid = lo + ((hi - lo) >> 1);
    if (feasible(mid)) hi = mid;
    else lo = mid + 1;
  }
  return lo;
}