Binary Search on Answer
Imagine you're a delivery driver who must finish a route before 6 PM. You can drive at different speeds. Drive too slow — you miss the deadline. Drive fast enough — you make it. Somewhere between 'too slow' and 'fast enough' there's a minimum speed that just barely works.
How do you find it? You could test every possible speed from 1 km/h upward — but that's painfully slow. A smarter move: test the middle speed. If that works, try slower. If it doesn't, try faster. You're doing binary search — not on an array, but on the space of possible answers.
The big idea
Regular binary search finds a value in a sorted array. Binary search on answer finds a value across the space of possible solutions to your problem. The trick only works when your answer space has a clean boundary: below some threshold the answer is "no, not feasible", and above it the answer is "yes, feasible" — always. This property is called monotonicity.
If you can write a function feasible(x) that answers "can we do it with x?", and if that function flips from false to true at exactly one point, then you can binary search for that flip point.
The template
Set lo to the minimum possible answer, hi to the maximum. Loop while lo < hi: compute mid = lo + (hi - lo) / 2. If feasible(mid) is true, the answer could be mid or something even smaller — set hi = mid. Otherwise, mid is too small — set lo = mid + 1. When the loop ends, lo == hi is your minimum feasible answer.
Classic examples
Split array into k groups (minimize max sum): Given an array and k groups, minimize the maximum group sum. For a candidate max sum x, greedily check if you can form k groups each summing to ≤ x. Binary search on x in range [max element, total sum]. Total: \(O(n · \log(total sum)\)).
Koko eating bananas: Piles of bananas, must finish in H hours. Find the minimum eating rate k. feasible(k) = can she finish all piles at rate k in H hours? Binary search on k in [1, max pile]. Total: \(O(n · \log(max pile)\)).
Integer square root: Find ⌊√n⌋ without floating point. feasible(x) = \(x^2\) ≤ n. Binary search on x in [0, n]. Total: \(O(\log n)\).