> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Candies Allocated to K Children

> Tested Python solution for LeetCode 2226 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 2226, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/maximum-candies-allocated-to-k-children/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2226   # by problem number
lcpy gen -s maximum_candies_allocated_to_k_children   # by problem name
```

## 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

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

```
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 \<= 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_candies_allocated_to_k_children/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_candies_allocated_to_k_children/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * log(max(candies)))
    # Space: O(1)
    def maximum_candies(self, candies: list[int], k: int) -> int:
        lo, hi = 1, max(candies)
        while lo <= hi:
            mid = (lo + hi) // 2
            if sum(pile // mid for pile in candies) >= k:
                lo = mid + 1
            else:
                hi = mid - 1
        return hi
```

## Complexity

| Time | Space |
| - | - |
| O(n \* log(max(candies))) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.