Maximum Candies Allocated to K Children
Medium
Topics
Given an array nums of candy piles and an integer target (= k children), split piles (never merge) so every child gets the same number of candies from a single sub-pile. Return the maximum number of candies each child can get; return 0 if it is impossible to give every child at least one candy.
Example 1
Input: nums = [5,8,6], target = 3 Output: 5
Example 2
Input: nums = [2,5], target = 11 Output: 0
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^7
- 1 <= target <= 10^12
Editorial — intuition, approach, complexity and pitfalls
Intuition
The answer is monotone: if every child can be given x candies, then every child can certainly be given x - 1. That monotonicity is the license to binary search the answer rather than search the space of allocations.
The feasibility test is a one-line sum. With a per-child portion of x, a pile of v candies yields v // x whole portions — the remainder is wasted, because piles may be split but never merged. The allocation is feasible when those portions reach target.
Approach
- Set
lo = 1andhi = max(nums). No child can receive more than the largest single pile, since piles are never combined. - For the midpoint
x, computesum(v // x)over all piles. - If that reaches
target, recordxand search the upper half; otherwise search the lower half. - Return the last feasible value, or 0 if there never was one.
Dry Run
nums = [5,8,6] target = 3 hi starts at max(nums) = 8
lo hi mid piles // mid children feasible
1 8 4 1+2+1 4 yes -> record 4, go higher
5 8 6 0+1+1 2 no -> go lower
5 5 5 1+1+1 3 yes -> record 5
answer 5Complexity
- Time — O(n log(max(nums))): the search space is bounded by the largest pile, so about
log₂(10⁷) ≈ 24iterations, each a linear feasibility sum. - Space — O(1): only the bounds and a running sum.
Common Pitfalls
- Setting
hi = sum(nums). Piles are never merged, so anyxabovemax(nums)gives every pile zero portions. The search still terminates but wastes iterations on a region that is provably infeasible. - Overflow in the feasibility sum.
targetreaches 10¹², which does not fit in a 32-bit integer; the shown Java returnslongfromkidsfor exactly this reason. - Starting
loat 0, which divides by zero on the first probe. - Treating the
sum(nums) < targetearly return as load-bearing. It is not. Removing it and comparing against the shown solution over 20,000 random inputs shows no divergence, becausekids(1) = sum(nums)already falls short oftarget, every midpoint is infeasible, and the loop returns 0 on its own. Keep the line for clarity, but do not defend it as a correctness fix. - Binary searching the number of children instead of the candies per child. The monotone quantity is the portion size.
Full Solution — all languages
class Solution:
def maximumCandies(self, nums, target):
if sum(nums) < target:
return 0
def kids(x):
s = 0
for v in nums:
s += v // x
return s
lo, hi, ans = 1, max(nums), 0
while lo <= hi:
mid = (lo + hi) // 2
if kids(mid) >= target:
ans = mid
lo = mid + 1
else:
hi = mid - 1
return ansimport java.util.*;
class Solution {
public int maximumCandies(int[] nums, int target) {
long total = 0;
for (int x : nums) total += x;
if (total < target) return 0;
int lo = 1, hi = 0, ans = 0;
for (int x : nums) hi = Math.max(hi, x);
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (kids(nums, mid) >= target) {
ans = mid;
lo = mid + 1;
} else hi = mid - 1;
}
return ans;
}
long kids(int[] nums, int x) {
long s = 0;
for (int v : nums) s += v / x;
return s;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int maximumCandies(vector<int>& nums, int target) {
long long total = 0;
for (int x : nums) total += x;
if (total < target) return 0;
int lo = 1, hi = 0, ans = 0;
for (int x : nums) hi = max(hi, x);
auto kids = [&](int x) {
long long s = 0;
for (int v : nums) s += v / x;
return s;
};
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (kids(mid) >= target) {
ans = mid;
lo = mid + 1;
} else hi = mid - 1;
}
return ans;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var maximumCandies = function(nums, target) {
let total = 0;
for (const x of nums) total += x;
if (total < target) return 0;
let lo = 1, hi = 0, ans = 0;
for (const x of nums) hi = Math.max(hi, x);
const kids = (x) => {
let s = 0;
for (const v of nums) s += Math.floor(v / x);
return s;
};
while (lo <= hi) {
const mid = (lo + hi) >> 1;
if (kids(mid) >= target) {
ans = mid;
lo = mid + 1;
} else hi = mid - 1;
}
return ans;
};