LeetCode 2226, Medium. Topics: Array, Binary Search. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:
Problem
You are given a <strong>0-indexed</strong> integer array <code>candies</code>. Each element in the array denotes a pile of candies of size <code>candies[i]</code>. You can divide each pile into any number of <strong>sub piles</strong>, but you <strong>cannot</strong> merge two piles together.
You are also given an integer <code>k</code>. You should allocate piles of candies to <code>k</code> children such that each child gets the <strong>same</strong> number of candies. Each child can be allocated candies from <strong>only one</strong> pile of candies and some piles of candies may go unused.
Return <em>the <strong>maximum number of candies</strong> each child can get.</em>
Examples
Constraints
- 1 <= candies.length <= 10<sup>5</sup>
- 1 <= candies[i] <= 10<sup>7</sup>
- 1 <= k <= 10<sup>12</sup>
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026