VOOZH about

URL: https://www.geeksforgeeks.org/dsa/koko-eating-bananas/

⇱ Koko Eating Bananas - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Koko Eating Bananas

Last Updated : 12 Aug, 2025

Given an array arr[] where each element denotes the number of bananas in a pile, and an integer k representing the total number of hours available. In one hour, up to x bananas can be eaten from a single pile. If a pile contains fewer than x bananas, the entire pile is consumed in one hour. It is guaranteed that the number of piles is less than or equal to k.
Determine the minimum value of x such that all piles can be completed within k hours.

Examples:

Input: arr[] = [5, 10, 3], k = 4
Output: 5
Explanation: If Koko eats at the rate of 5 bananas per hour.
=> First pile of 5 bananas will be finished in 1 hour.
=> Second pile of 10 bananas will be finished in 2 hours.
=> Third pile of 3 bananas will be finished in 1 hours.
Therefore, Koko can finish all piles of bananas in 1 + 2 + 1 = 4 hours.

Input: arr[] = [5, 10, 15, 20], k = 7
Output: 10
Explanation: If Koko eats at the rate of 10 bananas per hour, it will take 6 hours to finish all the piles.

[Naive Approach] Using Linear Search - O(n × m) time and O(1) space

The idea is to try every possible eating speed from 1 to max(arr) and, for each speed, calculate the total time required to finish all piles.
For a given speed x, the time to finish a pile is (pile + x - 1) // x, and we sum this over all piles.
The smallest speed for which the total time is less than or equal to k is the required answer.


Output
5

[Expected Approach] Using Binary Search on Answer

The core idea is to use binary search on the answer—specifically, on the eating speed x. For a given speed x, we can compute how many hours it would take to eat all the piles. This function is monotonic because -

  • As x increases, the total time required decreases or stays the same.
  • Therefore, if a certain speed x allows completion within k hours, any higher speed will also work

This monotonic behavior allows us to apply binary search to efficiently find the minimum valid speed.

Step by Step Implementation:

  • Initialize the binary search range:
    => Set low = 1 because the minimum possible eating speed is 1 banana per hour.
    => Set high = max(arr) since Koko never needs to eat faster than the largest pile.
    => Initialize a variable answer to store the minimum valid speed found.
  • Start binary search loop: While low <= high:
    => Calculate the middle speed: mid = (low + high) // 2
    - Initialize totalHours = 0
    - Loop through each pile in arr: For each pile, compute time required as (pile + mid - 1) // mid, add this time to totalHours
  • Decision based on total hours:
    => If totalHours <= k, then mid is a valid speed: Store mid in answer (it could be the minimum) and reduce the search space to the left half: high = mid - 1
    => If totalHours > k, then mid is too slow: Shift search to the right half: low = mid + 1
  • After the loop ends, answer contains the minimum speed that allows finishing all piles within k hours.

Output
5

Time Complexity: O(n × log m), binary search runs in O(log m) time, where m is the maximum pile size. For each candidate speed, we compute the total hours by iterating over all n piles, giving O(n) work per step.
Auxiliary Space: O(1)

Comment
Article Tags: