Find the maximum number of candies each of k children can receive if you may split piles into equal-sized portions.
You are given an array of candy pile sizes, where each pile contains a non-negative number of candies. You may divide the candies from the piles into equal-sized portions and give portions to children.
Your task is to determine the largest integer number of candies that can be given to each of the k children so that every child receives the same amount, and the candies used come only from the given piles.
A child must receive exactly one equal portion size, and a pile can contribute candies to multiple children only after being split into portions of that size. Any leftover candies that do not form a full portion are discarded.
If it is impossible to give at least 1 candy to each of the k children, return 0.
The goal is to maximize the candies per child.
Input Format
- An integer array
candieswherecandies[i]is the size of the i-th pile. - An integer
k, the number of children.
Assume the array length and values are suitable for standard integer processing.
Output Format
- Return a single integer: the maximum equal number of candies each child can receive.
Constraints
0 <= candies[i]1 <= k- The answer is an integer.
- If no positive allocation is possible, return
0.
Example 1
Input
candies = [5, 8, 6], k = 3
Output
5
Explanation
If each child gets 5 candies, we can form 1 portion from 5, 1 from 8, and 1 from 6, giving 3 children total. A larger equal amount is not possible for all 3 children.
Example 2
Input
candies = [2, 5], k = 11
Output
0
Explanation
Even if we split every pile into portions of size 1, we only have 7 candies total, so 11 children cannot each receive at least 1 candy.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.