Maximum Candies Allocated to K Children

Medium

Topics
ArrayBinary Search

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

508162
Input:  nums = [5,8,6], target = 3
Output: 5

Example 2

2051
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

  1. Set lo = 1 and hi = max(nums). No child can receive more than the largest single pile, since piles are never combined.
  2. For the midpoint x, compute sum(v // x) over all piles.
  3. If that reaches target, record x and search the upper half; otherwise search the lower half.
  4. 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 5

Complexity

  • Time — O(n log(max(nums))): the search space is bounded by the largest pile, so about log₂(10⁷) ≈ 24 iterations, 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 any x above max(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. target reaches 10¹², which does not fit in a 32-bit integer; the shown Java returns long from kids for exactly this reason.
  • Starting lo at 0, which divides by zero on the first probe.
  • Treating the sum(nums) < target early return as load-bearing. It is not. Removing it and comparing against the shown solution over 20,000 random inputs shows no divergence, because kids(1) = sum(nums) already falls short of target, 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 ans
SensAI
Beta
Tap the mic to stop voice mode