FastPrepMaximum Candies per Child

Maximum Candies per Child

Amazon logoAmazon● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

You are given positive candy-pile sizes and a positive number of children. A pile may be split into smaller portions, but portions from different piles may not be combined.

Every child must receive the same positive number of candies from one portion. Return the maximum candies each child can receive. Return 0 when the piles cannot give every child even one candy.

Function

maximumCandiesPerChild(piles: int[], children: long) → int

Examples

Example 1

piles = [5,8,6]children = 3return = 5

Portions of sizes 5, 5, and 5 can be formed; size 6 produces only two portions.

Example 2

piles = [2,5]children = 11return = 0

Only seven one-candy portions exist.

Constraints

  • 1 <= piles.length <= 200000
  • 1 <= piles[i] <= 10^9
  • 1 <= children <= 10^14

Source note: Interview prompt excerpt preserved as source evidence.

More Amazon problems

See Amazon hiring insights
public int maximumCandiesPerChild(int[] piles, long children) {
    // Write your code here.
}
piles[5,8,6]
children3
expected5
Checking account…