Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 be allocated candies from only 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 summary: 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 be allocated candies from only one pile of candies and some piles of candies may go unused. Return the maximum number of candies each child can get.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[5,8,6] 3
[2,5] 11
koko-eating-bananas)minimum-limit-of-balls-in-a-bag)minimum-speed-to-arrive-on-time)maximum-number-of-removable-characters)minimized-maximum-of-products-distributed-to-any-store)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2226: Maximum Candies Allocated to K Children
class Solution {
public int maximumCandies(int[] candies, long k) {
int l = 0, r = Arrays.stream(candies).max().getAsInt();
while (l < r) {
int mid = (l + r + 1) >> 1;
long cnt = 0;
for (int x : candies) {
cnt += x / mid;
}
if (cnt >= k) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
}
// Accepted solution for LeetCode #2226: Maximum Candies Allocated to K Children
func maximumCandies(candies []int, k int64) int {
return sort.Search(1e7, func(v int) bool {
v++
var cnt int64
for _, x := range candies {
cnt += int64(x / v)
}
return cnt < k
})
}
# Accepted solution for LeetCode #2226: Maximum Candies Allocated to K Children
class Solution:
def maximumCandies(self, candies: List[int], k: int) -> int:
l, r = 0, max(candies)
while l < r:
mid = (l + r + 1) >> 1
if sum(x // mid for x in candies) >= k:
l = mid
else:
r = mid - 1
return l
// Accepted solution for LeetCode #2226: Maximum Candies Allocated to K Children
/**
* [2226] Maximum Candies Allocated to K Children
*
* 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 be allocated candies from only 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 <= 10^5
* 1 <= candies[i] <= 10^7
* 1 <= k <= 10^12
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-candies-allocated-to-k-children/
// discuss: https://leetcode.com/problems/maximum-candies-allocated-to-k-children/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_candies(candies: Vec<i32>, k: i64) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2226_example_1() {
let candies = vec![5, 8, 6];
let k = 3;
let result = 5;
assert_eq!(Solution::maximum_candies(candies, k), result);
}
#[test]
#[ignore]
fn test_2226_example_2() {
let candies = vec![2, 5];
let k = 11;
let result = 0;
assert_eq!(Solution::maximum_candies(candies, k), result);
}
}
// Accepted solution for LeetCode #2226: Maximum Candies Allocated to K Children
function maximumCandies(candies: number[], k: number): number {
let [l, r] = [0, Math.max(...candies)];
while (l < r) {
const mid = (l + r + 1) >> 1;
const cnt = candies.reduce((acc, cur) => acc + Math.floor(cur / mid), 0);
if (cnt >= k) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.