Binary search on the answer
Sometimes there is no sorted array to search, but the answer itself is a number in a known range, and you can check any guess quickly. If every guess above the answer passes and every guess below fails (or the other way round), binary search the guesses instead of the data.
How to recognise it
- The question says "minimum speed", "smallest capacity", "least days", "maximum distance", "minimise the largest" or "maximise the smallest".
- Checking one guess is easy (often one greedy pass), but finding the best value directly is hard.
- Making the guess bigger only ever makes the check easier (or only ever harder). That is the monotonic property.
- The answer lies in a range you can write down, such as 1 to the largest value, or the largest value to the total sum.
The idea in one picture
Imagine a row of light switches labelled 1, 2, 3 and so on, one for every possible answer. Each switch is off if that
guess fails the check and on if it passes. If the row looks like off, off, off, on, on, on, the best answer is the first
switch that is on, and binary search finds it in log₂(range) checks. No sorted array is needed, only that row of
switches with one change.
Two shapes: smallest that works, largest that works
| Shape | Typical words | Row of guesses | Template |
|---|---|---|---|
| Smallest guess that passes | minimum speed, least days, smallest divisor | fail … fail pass … pass | if pass(mid): hi = mid else lo = mid + 1 |
| Largest guess that passes | maximum distance, highest blade | pass … pass fail … fail | if pass(mid): lo = mid else hi = mid - 1, with mid rounded up |
"Minimise the largest" problems (Split Array, Painter's Partition) are the first shape: guess a limit, and check
whether the work can be split into at most k parts that each stay under it.
Template
lo = smallest possible answer, hi = largest possible answer
while lo < hi:
mid = lo + (hi - lo) / 2
if check(mid): hi = mid # mid works, maybe something smaller does too
else: lo = mid + 1 # mid fails, so everything below it fails too
return lo
The real work is writing check. It is usually one greedy pass over the input:
| Problem | Guess | Check |
|---|---|---|
| Koko Eating Bananas | speed k | total hours Σ ceil(pile / k) ≤ h |
| Minimum days for bouquets | day d | enough runs of k bloomed flowers |
| Aggressive Cows | distance d | greedy places at least k cows |
| Split Array, Painter's Partition | limit L | greedy needs at most k parts |
| EKO | blade height H | wood cut Σ max(0, h - H) ≥ m |
Common mistakes
- Choosing a range that does not contain the answer, such as starting at 0 for a speed, which divides by zero.
- Rounding
middown in the "largest that passes" template, which loops forever when two guesses are left. - Overflowing in
check: sums of hours, wood or products often need 64-bit numbers. - Binary searching when the check is not monotonic. Test a few guesses by hand first.
Problems
- Sqrt(x)easyguide ready
- Find Nth root of a numbermediumguide ready
- Find the Smallest Divisor Given a Thresholdmediumguide ready
- Koko Eating Bananasmediumguide ready
- Minimum Number of Days to Make m Bouquetsmediumguide ready
- Aggressive Cowsmediumguide ready
- Rasta and Kheshtakmediumguide ready
- Kth smallest number againmediumguide ready
- EKOSPOJmediumguide ready
- Smallest number with atleastn trailing zeroes infactorialmediumguide ready
- ROTI-Prata SPOJmediumguide ready
- Split Array Largest Sumhardguide ready
- Minimize Max Distance to Gas Stationhardguide ready
- Optimum location of point to minimize total distancehardguide ready
- Painters Partition Problemhardguide ready