You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together.
You are also given an integer k. You should allocate piles of candies to k children such that each child gets the same number of candies. Each child can take at most one pile of candies and some piles of candies may go unused.
Return the maximum number of candies each child can get.
Example 1:
Input: candies = [5,8,6], k = 3 Output: 5 Explanation: We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.
Example 2:
Input: candies = [2,5], k = 11 Output: 0 Explanation: There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.
Constraints:
1 <= candies.length <= 1051 <= candies[i] <= 1071 <= k <= 1012Problem Overview: You receive an array candies where each element represents a pile of candies. You can split a pile into smaller piles but cannot combine piles. The goal is to distribute candies to k children so every child receives the same number of candies. Return the maximum candies each child can get.
Approach 1: Brute Force Enumeration (Time: O(n * M), Space: O(1))
A direct approach is to try every possible number of candies per child from 1 up to the largest pile size M. For each candidate value x, iterate through the array and compute how many children can be served using pile // x. If the total count is at least k, the distribution works. Track the largest valid x. This approach performs a full array scan for every possible candy count, which becomes too slow when the maximum pile value is large.
Approach 2: Binary Search on Answer (Time: O(n log M), Space: O(1))
The key observation: if you can give x candies per child, then any value smaller than x is also feasible. This monotonic property makes the problem ideal for binary search. Instead of testing every possible value, search the range [1, max(candies)]. For each midpoint mid, iterate through the array and compute how many children can be served using pile // mid. If the total number of children served is at least k, increase the lower bound to search for a larger answer. Otherwise reduce the upper bound.
The counting step is straightforward: loop through the array and accumulate pile // mid. This represents how many equal portions of size mid each pile can contribute. The algorithm repeatedly narrows the search space until the largest feasible value is found.
This method avoids scanning every possible candy amount. Instead, it performs roughly log(max(candies)) checks, each requiring a single pass through the array. With large pile sizes (up to millions or billions), this improvement is substantial.
Recommended for interviews: Binary search on the answer is the expected solution. Interviewers look for recognition of the monotonic feasibility condition and the ability to convert the problem into a search space over possible answers. Mentioning the brute force approach first shows understanding of the constraint, while the binary search optimization demonstrates strong algorithmic reasoning.
The binary search approach involves searching for the maximum number of candies each child can get. We perform a binary search on the possible number of candies from 0 up to the largest pile of candies. For each candidate number of candies in our search, we check if it is feasible to allocate that number of candies to each of the k children. This feasibility check is done by attempting to distribute the candies such that each child receives the desired number.
By checking the feasibility at each step, we narrow down our search to the largest feasible number of candies per child.
The C solution uses a helper function canDistribute to verify the feasibility of distributing a certain number of candies to each child. We perform a binary search where the middle candy count is checked. If feasible, the result is updated to the mid-value and the search continues on the higher half. If not feasible, the search continues on the lower half. This process continues until the entire search space is exhausted. The final result will be the maximum number of candies each child can receive.
Time Complexity: O(n * log(m)), where n is the number of piles and m is the max candies available in any pile.
Space Complexity: O(1), as no additional space is used proportional to input size.
We notice that if each child can receive v candies, then for any v' \lt v, each child can also receive v' candies. Therefore, we can use binary search to find the maximum v such that each child can receive v candies.
We define the left boundary of the binary search as l = 0 and the right boundary as r = max(candies), where max(candies) represents the maximum value in the array candies. During the binary search, we take the middle value v = \left\lfloor \frac{l + r + 1}{2} \right\rfloor each time, and then calculate the total number of candies each child can receive. If the total is greater than or equal to k, it means each child can receive v candies, so we update the left boundary l = v. Otherwise, we update the right boundary r = v - 1. Finally, when l = r, we have found the maximum v.
The time complexity is O(n times log M), where n is the length of the array candies, and M is the maximum value in the array candies. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Binary Search Approach | Time Complexity: O(n * log(m)), where n is the number of piles and m is the max candies available in any pile. |
| Binary Search | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n * M) | O(1) | Conceptual baseline or when maximum pile size is very small |
| Binary Search on Answer | O(n log M) | O(1) | Optimal solution for large arrays and large pile values |
Maximum Candies Allocated to K Children - Leetcode 2226 - Python • NeetCodeIO • 11,139 views views
Watch 9 more video solutions →Practice Maximum Candies Allocated to K Children with our built-in code editor and test cases.
Practice on FleetCode