Loading…
Loading…
Charting your lesson…
Opening your lesson…
Binary Search on the Answer
Lesson 6 of 7
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
The most powerful use of binary search isn't searching an array at all — it's searching the space of possible answers. When a problem asks for the minimum/maximum value that satisfies a monotonic condition, you can binary-search the answer even with no sorted array in sight.
Core idea: if a predicate feasible(x) is monotonic — false, false, …, true, true (or the reverse) — then binary search finds the boundary value (the smallest feasible x, or largest) in O(log(range) × cost-of-check), without enumerating every candidate.
Step 2/ 61 min
Define a boolean feasible(x) that is monotonic in x, then binary-search the answer range:
lo, hi = minPossible, maxPossible
while lo < hi:
mid = lo + (hi - lo)//2
if feasible(mid): hi = mid # mid works; try smaller
else: lo = mid + 1 # mid too small; go higher
return lo # smallest feasible answer
The array isn't searched — the answer axis is.
The trick works when "if x works, every larger (or smaller) x also works". Examples:
Total time = O(log(hi − lo)) × O(check). The check (feasible) is usually an O(n) greedy simulation, so overall O(n log(range)) — dramatically better than O(range·n) brute force. The two design steps: (1) identify the monotonic predicate, (2) write an efficient feasibility check.
Step 3/ 61 min
Koko eats n piles; find the min speed to finish in H hours. feasible(speed) = "total hours at this speed ≤ H", computed by summing ceil(pile/speed) — O(n), and monotonic (faster speed → fewer hours). Binary-search speed in [1, max(pile)]: each mid runs the O(n) check, total O(n log(maxPile)). The boundary where feasible flips false→true is the answer.
Step 4/ 61 min
Step 5/ 61 min
lo/hi to the true answer bounds (e.g. max element to total sum for partition problems).lo after the lo < hi / hi = mid loop for the smallest feasible answer.Binary-searching the answer when the feasibility predicate is not monotonic. If feasible flips true/false more than once across the range, halving discards valid answers and returns garbage. Confirm monotonicity first; otherwise this technique doesn't apply.
Step 6/ 6· Worked examples1 min
Problem
Koko eats bananas from piles [3, 6, 7, 11]. At speed k bananas/hour she needs Σ ceil(pile / k) hours. Find the minimum integer speed k so she finishes within h = 8 hours.
Try it on paper first.
Recall
Keep this
Usage: frame the problem as "smallest x for which feasible(x) is true", then binary search x.
"Binary search on the answer" is the pattern where you binary-search over the range of possible answers rather than an array — applicable whenever a candidate answer has a monotonic feasibility (if x works, everything larger/smaller works). It solves minimise-the-maximum and maximise-the-minimum problems that look nothing like search at first glance.
Identify a value range [lo, hi] for the answer and a boolean feasibility check feasible(mid) that is monotonic. Binary-search the smallest (or largest) feasible value: if feasible(mid), move toward the optimum, else move away. Cost is O(log(range) × cost of feasible). The hard part is spotting the monotonicity and writing feasible() — often a greedy O(n) simulation.
| Problem | Search over | feasible(x) | Time |
|---|---|---|---|
| Koko eating bananas | eating speed | finishes in ≤ h hours | O(n log(max)) |
| Ship packages in D days | capacity | fits in ≤ D days | O(n log(sum)) |
| Split array largest sum | max subarray sum | ≤ m subarrays | O(n log(sum)) |
| Minimise max distance | distance | achievable | O(n log(range)) |
Koko eating bananas, capacity to ship packages in D days, split array largest sum, minimum days to make bouquets, or "smallest divisor given a threshold". Interviewers expect you to spot monotonicity and code the feasibility check.
When the answer has a monotonic feasibility test, binary-search the answer range instead of an array. Write a greedy O(n) feasible(mid); total O(n log(range)). It cracks minimise-max / maximise-min optimisation problems.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.