intermediate10 min read·Updated October 7, 2026

Binary Search on Answer Explained: Koko Eating Bananas

Master binary search on the answer with Koko eating piles [3,6,7,11] in 8 hours. Build the mental model, trace speed k=4, and handle edge cases.

By Learnisim AI·Published October 7, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • Basic binary search on arrays
  • Array iteration and division ceiling logic
  • Big-O time complexity basics
Binary Search on the Answer: Koko Eating Bananas (piles = [3, 6, 7, 11], h = 8) Input Piles & Constraint 3 6 7 11 Time Limit: h = 8 hours Min speed k such that hours ≤ 8 Monotonic Search Space for k 1 lo (min) 6 mid (checked) 11 hi (max pile) Binary Search Execution Trace (Step-by-Step) Iteration 1: mid = 6 Bounds: lo = 1, hi = 11 Ceil Division Simulation: ⌈3/6⌉ + ⌈6/6⌉ + ⌈7/6⌉ + ⌈11/6⌉ = 1 + 1 + 2 + 2 = 6 hours 6 ≤ 8 (Valid!) Finished in time. Record ans = 6. Action: Try smaller speed to find minimum. Set hi = mid = 6 New range: [1, 6] Iteration 2: mid = 3 Bounds: lo = 1, hi = 6 Ceil Division Simulation: ⌈3/3⌉ + ⌈6/3⌉ + ⌈7/3⌉ + ⌈11/3⌉ = 1 + 2 + 3 + 4 = 10 hours 10 > 8 (Too Slow!) Exceeds time limit. Cannot finish. Action: Need faster speed; eliminate lower half. Set lo = mid + 1 = 4 New range: [4, 6] Iteration 3: mid = 4 Bounds: lo = 4, hi = 6 Ceil Division Simulation: ⌈3/4⌉+⌈6/4⌉+⌈7/4⌉+⌈11/4⌉ = 1 + 2 + 2 + 3 = 8 hours 8 ≤ 8 (Exact Match!) Valid solution found. Result: Optimal speed minimized: k = 4 bananas / hour O(N log M) complexity achieved
Koko: piles [3, 6, 7, 11], h = 8 overview diagram
Why

Why Brute Force Fails When Eating Bananas at Scale

Phase 1: The Guarded Banana Pile

Imagine you are Koko, and you love bananas. You are looking at a line of four piles with sizes bananas, and the guards will return in hours. You can choose to eat at a constant speed of bananas per hour. If a pile has fewer than bananas, you finish it in that hour and stop eating for the rest of that hour. If you eat at a speed of , you will take hours, which vastly exceeds your 8-hour limit. If you eat at a blazing speed of , you finish each pile in 1 hour, taking 4 total hours—well under the limit, but you waste a lot of energy chewing so fast.
To find the absolute minimum integer speed that still lets you finish in hours, your first instinct might be to test every single speed from 1 up to the maximum pile size of 11. You would simulate speed 1, then speed 2, then speed 3, and so on until you hit , which takes exactly 8 hours. While this linear scan works for tiny inputs, what happens if the guards give you piles reaching bananas and hours? A linear search would perform a billion iterations, timing out instantly.
We need a way to rule out entire blocks of speeds at once rather than checking them one by one. Because the total hours needed decrease monotonically as our speed increases, we can leverage the hidden order of the solution space instead of stumbling through it blindly.
Why Brute Force Fails: Koko Eating Bananas (Piles: [3, 6, 7, 11], h = 8) 1. The Guarded Piles & Constraints Limit: h = 8 hours | Target: find min speed k Pile 1 3 Pile 2 6 Pile 3 7 Max Pile 11 (k_max) 2. Why Brute Force Linear Scan Fails Testing speeds k = 1, 2, 3 ... one by one k = 1 27 hrs k = 2 14 hrs k = 3 10 hrs k = 4 (Ans) 8 hrs (Valid!) ... to k = 10^9 Time Out! Bottleneck: Checking up to 10^9 piles takes 10^9 operations! 3. The Monotonic Solution Space & Binary Search (Log N) Hours needed strictly decrease as speed k increases. We binary search range [1, 11]. Left = 1 Mid = 6 (Too Fast) Right = 11 Eliminate entire right half [7...11] instantly! Scale Result: Reduces O(max_pile) linear scans down to O(log(max_pile)) — handles 10^9 in ~30 steps!
Why Brute Force Fails When Eating Bananas at Scale diagram
Model

Framing the Eating Speed as a Monotonic Search Space

Phase 2: Mapping Piles to a Monotonic Range

When Koko faces piles = [3, 6, 7, 11] and h = 8, she doesn't need to test every single speed from 1 to 11 one by one. Instead, we can think of her eating speed as a range of candidate values. The minimum possible speed is 1 (she eats at least one banana per hour), and the maximum possible speed is the largest pile, which is 11 (eating faster than the largest pile yields no time-saving benefit since she can only eat from one pile per hour).
This range has a powerful property: monotonicity. If Koko can successfully finish all bananas at speed , she can also finish them at any speed greater than 4 (like ). Conversely, if she fails at , she will fail at any slower speed. Whenever a search space is sorted and monotonic, linear scanning is unnecessary—we can use binary search.
To evaluate any candidate speed , we simulate her eating process across our specific piles: . For each pile of size , the hours required at speed is given by the ceiling division . Summing these up gives the total hours needed for that speed.
Binary Search on Koko's Eating Speed (Piles: [3, 6, 7, 11], h = 8) Range [1, 11] is monotonic: if speed k works, any speed > k also works. 1. Bananas Piles (p) Pile 1 3 Pile 2 6 Pile 3 7 Pile 4 (Max) 11 2. Monotonic Search Space [Min=1, Max=11] Speed k: 1 2 3 Mid 4 5 ... 11 Monotonicity Valid ≥ 4 | Fail < 4 3. Simulation Engine: Testing Candidate Speed k = 4 (Hours required = ⌈p / k⌉) Pile 1: size = 3 ⌈ 3 / 4 ⌉ = 1 hour Done in 1h Pile 2: size = 6 ⌈ 6 / 4 ⌉ = 2 hours Done in 2h Pile 3: size = 7 ⌈ 7 / 4 ⌉ = 2 hours Done in 2h Pile 4: size = 11 ⌈ 11 / 4 ⌉ = 3 hours Done in 3h Total Hours Needed: 1 + 2 + 2 + 3 = 8h Target h = 8: Valid! (≤ 8h) Action: Try Left Half: Search [1, 3]
Framing the Eating Speed as a Monotonic Search Space diagram
Worked example

Tracing Binary Search on Koko's Eating Speed Step by Step

Phase 3: Worked Example

Having established that speed forms a clean monotonic range, we now trace the binary search procedure end-to-end on our locked working example. We are given piles and a time limit hours. Our goal is to find the minimum integer bananas per hour that allows Koko to finish all bananas within 8 hours.

Given

- (four piles of sizes 3, 6, 7, and 11)
- hours
- Search bounds: (minimum possible speed) and (maximum possible speed, since she can eat at most one pile per hour).

Steps

We iteratively compute the midpoint , calculate the total hours required at speed , and adjust our bounds.
•Iteration 1:

- .
- Compute hours needed at : hours.
- Since , Koko finishes in time! But can she go slower? We record 6 as a valid answer and try a smaller speed by setting .
•Iteration 2:

- .
- Compute hours needed at : hours.
- Since , 3 is too slow. Koko needs a higher speed, so we adjust .
•Iteration 3:

- .
- Compute hours needed at : hours.
- Since , 5 is valid! We record 5 and try a smaller speed by setting .
•Iteration 4:

- .
- Compute hours needed at : wait, let us trace exact integer math: hours? Let us check: , , , . Sum = hours.
- Since , 4 is valid! We set .
•Termination:

- Now and . The search space collapses.

Result

The minimum speed that satisfies is . Notice how testing speeds eliminated half the remaining search space at each step.
Try this: Given piles = [3, 6, 7, 11] and h = 8, suppose we evaluate speed k = 2. Exactly how many total hours does Koko require, and why does this rule out k = 2?
Phase 3 Trace: Piles [3, 6, 7, 11], h = 8 Hours Piles: 3 6 7 11 Target: h ≤ 8 hrs | Range: [1, 11] Iteration 1: mid = 6 Bounds: lo = 1, hi = 11 Hours at k = 6: ⌈3/6⌉ + ⌈6/6⌉ + ⌈7/6⌉ + ⌈11/6⌉ = 1 + 1 + 2 + 2 Total = 6 hours (≤ 8) Valid! Try smaller speed Action: hi = mid = 6 Iteration 2: mid = 3 Bounds: lo = 1, hi = 6 Hours at k = 3: ⌈3/3⌉ + ⌈6/3⌉ + ⌈7/3⌉ + ⌈11/3⌉ = 1 + 2 + 3 + 4 Total = 10 hours (> 8) Too slow! Need faster Action: lo = mid + 1 = 4 Iteration 3: mid = 5 Bounds: lo = 4, hi = 6 Hours at k = 5: ⌈3/5⌉ + ⌈6/5⌉ + ⌈7/5⌉ + ⌈11/5⌉ = 1 + 2 + 2 + 3 Total = 8 hours (≤ 8) Valid! hi = mid = 5 Result: min k = 4 found!
Tracing Binary Search on Koko's Eating Speed Step by Step diagram
Practice

Test Your Intuition: Predicting Midpoint Shifts on Modified Constraints

Phase 4: Practice

Now that you have traced the standard execution for piles = [3, 6, 7, 11] and h = 8 where the optimal speed is , let us test your grip on the search dynamics. Suppose we modify the target hours constraint to be much stricter: hours for the exact same pile sizes [3, 6, 7, 11].
Recall that the search bounds are initially and , and our hour-calculation function uses the ceiling division formula . Think about what happens at the very first midpoint, and whether Koko can finish all piles within 4 hours at that speed.

The Task

Given piles = [3, 6, 7, 11] and a strict time limit , evaluate the first iteration of the binary search:
1. What is the initial midpoint speed , and how many hours does Koko require at this speed?
2. Based on whether this hour total is or , which pointer (lo or hi) will move, and what is the new search interval for the next step?
Take a moment to write down or mentally trace your calculations before checking how the monotonicity invariant guides the update.
Try this: piles = [3, 6, 7, 11], h = 4
lo = 1, hi = 11
mid = (1 + 11) // 2 # Evaluate hours needed here
Apply

Recognizing When a Problem Maps to Binary Search on the Answer

Phase 5: Recognizing the Pattern in the Wild

We started with Koko facing piles and hours, where brute-force iteration through speeds was too slow. By realizing that speed has a monotonic property—if Koko can finish at speed 4, she can also finish at speed 5 or 11, but fails at 3—we transformed an unordered optimization problem into a binary search over a sorted range of answers.
This exact pattern appears whenever a problem asks for the minimum valid maximum or maximum valid minimum threshold, and checking a candidate value is fast. Consider a shipping company needing to transport packages of weights within days. Instead of guessing days, you guess the maximum daily weight capacity , and use a greedy pass to check if the fleet can finish within days.
Just like Koko's piles, if capacity works, any higher capacity also works, allowing us to shrink the upper bound (hi = mid). If capacity is too small, we must increase the lower bound (lo = mid + 1). Whenever you see a problem asking for the "minimum capacity", "smallest speed", or "earliest time" subject to a feasibility check, pause and ask: Can I binary search the answer?

FAQ

Why does binary search work on eating speeds for Koko eating bananas?
The search space of possible speeds (from 1 to the max pile size) is monotonic. If Koko can successfully eat all bananas at speed k, she can also do it at any speed greater than k. If she fails at speed k, she will fail at any smaller speed.
How does the algorithm find speed 4 for piles [3, 6, 7, 11] and h = 8?
We test a candidate speed like k=3, which takes 1+2+3+4=10 hours (too slow for h=8). Moving to k=4 takes 1+2+2+3=8 hours, which exactly meets the deadline. Since 4 is valid, we try to see if a slower valid speed exists before settling on 4.
What is the time complexity of binary search on the answer for this problem?
The time complexity is O(N log M), where N is the number of piles and M is the maximum size of a pile. The log M factor comes from the binary search range of speeds, and checking each speed takes O(N) time.
What is a common pitfall when implementing binary search on the answer?
A common mistake is incorrectly defining the lower and upper bounds of the search space (e.g., setting the minimum speed to 0 instead of 1, causing a division by zero error) or failing to narrow the range correctly when a valid speed is found.

Keep learning