Maximum Candies Allocated to K Children (LeetCode 2226): Solution and Explanation
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
- Set
lo = 0andhi = max(candies). A share bigger than the largest pile can never be cut. - While
lo < hi, testmid = (lo + hi + 1) // 2. Rounding up keeps the loop from stalling whenhi = lo + 1. - Count
piles = sum(c // mid)in a 64-bit integer. - If
piles >= k,midis achievable, so setlo = mid. Otherwise sethi = mid - 1. - Return
lo. It is 0 when there are fewer thankcandies in total.
Step-by-step example
candies = [5,8,6], k = 3
- lo = 0, hi = 8. mid = 4: shares 1 + 2 + 1 = 4 >= 3, so lo = 4.
- mid = 6: shares 0 + 1 + 1 = 2 < 3, so hi = 5.
- mid = 5: shares 1 + 1 + 1 = 3 >= 3, so lo = 5.
- 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 lo1class Solution {
2 public int maximumCandies(int[] candies, int k) {
3 int lo = 0, hi = 0;
4 for (int c : candies) hi = Math.max(hi, c);
5 while (lo < hi) {
6 int mid = lo + (hi - lo + 1) / 2;
7 long piles = 0;
8 for (int c : candies) piles += c / mid;
9 if (piles >= k) {
10 lo = mid;
11 } else {
12 hi = mid - 1;
13 }
14 }
15 return lo;
16 }
17}1class Solution {
2public:
3 int maximumCandies(vector<int>& candies, int k) {
4 int lo = 0, hi = *max_element(candies.begin(), candies.end());
5 while (lo < hi) {
6 int mid = lo + (hi - lo + 1) / 2;
7 long long piles = 0;
8 for (int c : candies) piles += c / mid;
9 if (piles >= k) {
10 lo = mid;
11 } else {
12 hi = mid - 1;
13 }
14 }
15 return lo;
16 }
17};Time and space complexity
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.
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
loat 1 and never checking whether 1 is feasible. When there are fewer thankcandies the answer must be 0. - Using
mid = (lo + hi) // 2together withlo = mid. Whenhi = lo + 1this 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.