Maximum Candies Allocated to K Children

Medium Topics Company Tags

Each value in candies is the size of one pile. You may split piles into smaller piles, but cannot combine candies from different original piles. Give each of k children an equal number of candies, with each child receiving candies from just one original pile. Leftover candies may be unused. Return the largest possible number per child, or 0 if there are not enough candies to give everyone at least one.

Example 1:

Input: candies = [4,7]
k = 3

Output: 3

Explanation: Give one child 3 candies from the pile of 4 and two children 3 each from the pile of 7. A portion of 4 would yield only two portions, which is not enough.

Example 2:

Input: candies = [2,3]
k = 6

Output: 0

Explanation: There are only 5 candies for 6 children, so even one candy per child is impossible.

Example 3:

Input: candies = [8]
k = 2

Output: 4

Explanation: Split the pile of 8 into two portions of 4. Both children receive 4 candies, using the entire pile.

Constraints:

  • 1 <= candies.length <= 100000
  • 1 <= candies[i] <= 10000000
  • 1 <= k <= 1000000000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.