Pattern #05
Binary Search on Answer
EssentialBinary 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.
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
- Prove the monotone direction. On this path, if a guess works, every larger guess works.
- Set tight bounds. A bound that excludes the true answer cannot be repaired by the loop.
feasibleonly answers whether this guess is allowed. It does not moveloorhi.- Keep the template’s first-true shrink. A feasible
midsetshi = mid. Raisinglotomidsearches 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: 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;
}
- 1#410 Split Array Largest SumGuidehard
- 2#774 Minimize Max Distance to Gas StationGuidehard
- 3#875 Koko Eating BananasGuidemedium
- 4#1011 Capacity To Ship Packages Within D DaysGuidemedium
- 5#1283 Find the Smallest Divisor Given a ThresholdGuidemedium
- 6#1482 Minimum Number of Days to Make m BouquetsGuidemedium