Maximum Candies Allocated to K Children (LeetCode 2226): Solution and Explanation

Medium
Array
Binary Search

Piles of candies can be split but not merged. Give k children one pile each so that every child gets the same amount, and return the largest such amount, or 0 if even one candy each is impossible. For candies = [5,8,6] and k = 3 the answer is 5.

Intuition

Fix a share size x. A pile of c candies can be cut into c // x piles of exactly x, and the rest is wasted. So x is achievable when the sum of c // x over all piles is at least k.

If x works, any smaller share works too, because each pile only yields more pieces. The achievable shares form a range from 0 up to some best value.

That monotonic yes/no test is the signal for binary search on the answer: test the middle share, then throw away the half that cannot contain the best value.

Approach: Binary search on the share size

  1. Set lo = 0 and hi = max(candies). A share bigger than the largest pile can never be cut.
  2. While lo < hi, test mid = (lo + hi + 1) // 2. Rounding up keeps the loop from stalling when hi = lo + 1.
  3. Count piles = sum(c // mid) in a 64-bit integer.
  4. If piles >= k, mid is achievable, so set lo = mid. Otherwise set hi = mid - 1.
  5. Return lo. It is 0 when there are fewer than k candies in total.

Step-by-step example

candies = [5,8,6], k = 3
  1. lo = 0, hi = 8. mid = 4: shares 1 + 2 + 1 = 4 >= 3, so lo = 4.
  2. mid = 6: shares 0 + 1 + 1 = 2 < 3, so hi = 5.
  3. mid = 5: shares 1 + 1 + 1 = 3 >= 3, so lo = 5.
  4. lo = hi = 5. Each child gets 5 candies.

Code: Python, Java, C++

1class Solution:
2    def maximumCandies(self, candies: List[int], k: int) -> int:
3        lo, hi = 0, max(candies)
4        while lo < hi:
5            mid = (lo + hi + 1) // 2
6            piles = sum(c // mid for c in candies)
7            if piles >= k:
8                lo = mid
9            else:
10                hi = mid - 1
11        return lo
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log M)

The search range has size M = max(candies), so there are about log M probes, and each one scans all n piles.

Space

O(1)

Only the bounds and a running count are stored.

Other approaches

Try every share size (time O(n M), space O(1))

Test x = M, M - 1, ... down to 1 and stop at the first that serves k children. Correct but up to 10^7 probes of n piles each.

Tighter upper bound (time O(n log M), space O(1))

Start hi at min(max(candies), sum(candies) // k), since no share can exceed the average. It saves a few probes but not the complexity class.

Common mistakes

  • Counting shares in a 32-bit integer. With 10^5 piles of 10^7 candies and a share of 1, the count reaches 10^12.
  • Starting lo at 1 and never checking whether 1 is feasible. When there are fewer than k candies the answer must be 0.
  • Using mid = (lo + hi) // 2 together with lo = mid. When hi = lo + 1 this never moves and loops forever.

Frequently asked questions

Why can piles not just be merged to waste less?

The problem forbids merging. That is why a pile's leftover after cutting chunks of size x is lost, and why the count is c // x rather than the total divided by x.

Why does the search start at 0 instead of 1?

0 is the answer when even one candy per child is impossible. Keeping 0 inside the range means the loop returns it naturally, without a special case.

Is this the same pattern as Koko Eating Bananas?

Yes. Both binary search a rate or size over a range and use a greedy count to decide feasibility. Here larger values are harder, so the search keeps the largest feasible value instead of the smallest.