Skip to main content

Maximum Candies Allocated to K Children - Solution & Explanation

MediumArrayBinary Search17 min readAsked at: Amazon, Microsoft, Meta +3
Practice this problem

Problem Statement

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 <= 105
  • 1 <= candies[i] <= 107
  • 1 <= k <= 1012

Approach Overview

Problem 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.

Approach 1: Binary Search Approach

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Binary Search

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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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.
Space Complexity: O(1), as no additional space is used proportional to input size.

Binary Search—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n * M)O(1)Conceptual baseline or when maximum pile size is very small
Binary Search on AnswerO(n log M)O(1)Optimal solution for large arrays and large pile values

Video Solution

Maximum Candies Allocated to K Children - Leetcode 2226 - Python • NeetCodeIO • 11,539 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Maximum Candies Allocated to K Children easy or hard?
Maximum Candies Allocated to K Children is rated Medium difficulty. The implementation is simple once you recognize the binary search on answer pattern, but identifying the monotonic feasibility condition can be tricky for beginners.
Maximum Candies Allocated to K Children Python/Java solution
The standard implementation performs binary search between 1 and max(candies). For each midpoint, iterate through the array and accumulate pile // mid to count served children. This logic translates directly across Python, Java, C++, C#, and JavaScript with identical O(n log M) complexity.
How to solve Maximum Candies Allocated to K Children in O(n log M)?
Use binary search over the possible candies per child. Set the search range from 1 to the maximum pile size. For each midpoint value, iterate through the piles and compute how many children can be served using pile // mid. If the total is at least k, move the search to the right to try a larger value; otherwise move left.
What is the best approach for Maximum Candies Allocated to K Children?
Binary search on the answer is the most efficient approach. The idea is to search the maximum candies each child can receive in the range [1, max(candies)]. For each candidate value, iterate through the piles and count how many children can be served using integer division. This solution runs in O(n log M) time and O(1) space.
Is Maximum Candies Allocated to K Children asked at Google/Amazon/Meta?
Binary search on answer problems frequently appear in interviews at companies like Amazon, Google, and Meta. Variants involving resource allocation, maximizing minimum values, or splitting piles are common patterns used to test binary search reasoning and feasibility checks.
What data structure is used in Maximum Candies Allocated to K Children?
The main data structure is a simple array representing candy piles. The algorithm relies on binary search over the answer space and a linear scan of the array to compute how many children can be served for a given candidate value.
What is the time complexity of Maximum Candies Allocated to K Children?
The optimal solution runs in O(n log M) time, where n is the number of piles and M is the maximum number of candies in a pile. Each binary search step scans the array once to count how many children can receive the candidate amount. Space complexity remains O(1) since only counters and pointers are used.

Ready to solve this problem?

Practice Maximum Candies Allocated to K Children with our built-in code editor and test cases.

Practice on FleetCode