Binary Search
Binary Search on Answer
Solve optimization problems by applying binary search to the answer value range using monotonic feasibility checks.
Last Updated: August 2, 2026
•
20 min read
1. Introduction
What is Binary Search on Answer?
Binary Search on Answer is an optimization technique. Instead of searching for an index in a sorted array, we binary search over the range of all possible answers (e.g., minimum speeds, maximum capacities, or lowest costs).Why is it Important?
Many optimization problems (e.g., "minimize the maximum value") seem like they require complex dynamic programming or exhaustive search. However, if the feasibility of the answer is monotonic, we can formulate a decision check and use binary search to find the absolute boundary of feasibility inO(N log(range)) time.
Where is it Used?
2. Mental Model: The Feasibility Slider
Imagine a slider representing task execution capacity (e.g., conveyor speed).
1), the factory runs behind and tasks fail.10), the factory finishes way early, but at high cost.5). For any speed ≥ 5, we can successfully finish. For any speed < 5, we fail.Instead of guessing speed, we slide the setting to the middle, check if the factory can complete tasks, and adjust our range accordingly.
3. Core Concepts & Implementations
Monotonicity & Feasibility Check
Two conditions must be met to apply this pattern: 1. Monotonicity: If a capacityC works, any capacity > C must also work. If C fails, any capacity < C must also fail.
2. Fast Feasibility Check: We can write a helper function check(mid) (often using a greedy simulation) that returns true/false in O(N) time.
Two Classic Interview Problems
1. Koko Eating Bananas: Find the minimum eating speedK to eat all banana piles within H hours.
2. Capacity to Ship Packages: Find the minimum ship capacity to carry all packages within D days.4. Visual Trace: Shipping Packages [3, 2, 2, 4, 1] in 3 Days
lo = max(weights) = 4 (must be able to carry the heaviest package)hi = sum(weights) = 12 (carry everything in one day)5. Real-World Applications
X milliseconds while minimizing power.6. Interview Perspective
How Interviewers Ask This Topic
Look for problems with optimization prompts:M subarrays such that the maximum subarray sum is minimized." (Split Array Largest Sum -> equivalent to shipping packages!).Common Mistakes
Warning: 1. Incorrect lo Range Boundary: Initializing
lo = 1 or lo = 0 in package shipping instead of max(weights). If capacity is smaller than max(weights), we can never pack the heaviest item, leading to infinite loops or incorrect evaluations.> 2. Non-Monotonic Conditions: Trying to apply this pattern when the feasibility function is not monotonic (e.g. speedXworks, but speedX+1fails due to task resonance).
7. Summary
O(N) simulation check returning true/false.lo < hi) to locate the exact cutoff point.8. Quiz
Question 1: What is the benefit of Binary Search on Answer over standard search?
Answer: It converts an optimization problem ("find the absolute best value") into a decision problem ("is value X possible?"). Decision checks are usually simpleO(N) greedy passes, yielding an extremely efficient O(N log(range)) solution.
Question 2: In Koko Eating Bananas, why is the maximum speed upper bound initialized to max(piles)?
Answer: Because Koko can only eat from one pile per hour. Eating at a speed faster than the largest pile size still takes exactly 1 hour for that pile, so any speed greater thanmax(piles) is redundant.
Question 3: How do you perform ceiling division of integers (a / b) in Java/C++ without floating-point conversion?
Answer: Use the formula(a + b - 1) / b. For example, (11 + 3 - 1) / 3 = 13 / 3 = 4, which matches ceil(11.0 / 3.0) = 4.0.
Question 4: What is the time complexity of the "Split Array Largest Sum" problem on an array of size N with elements up to value V?
Answer:O(N log(Sum)), where Sum \approx N × V. The binary search takes log(Sum) steps, and each feasibility check runs in O(N) time.
Question 5: If the check function returns true for mid and we want to maximize the answer, how do we update the range boundaries?
Answer: If we want to maximize the answer andmid is feasible, then we should check if a larger value works. We set lo = mid (or lo = mid + 1 if using exclusive bounds), instead of hi = mid.