As reported: ServiceNow Senior Software Engineer (IC3, full stack) loop, round 2 (coding), Apr 2025: two problems — this one, solved with binary search on the answer, and the nearest smaller element to the left.
There are people people, type1 candies of the first type and type2 candies of the second type. Hand out all the candies so that:
- every person gets at least one candy, and
- no person gets candies of both types.
Let X be the smallest number of candies any person receives. Return the maximum possible X.
Example
people = 4, type1 = 4, type2 = 5 -> 2 (2 + 2 of type 1; 2 + 3 of type 2)
people = 5, type1 = 2, type2 = 3 -> 1
people = 4, type1 = 7, type2 = 10 -> 3
Constraints: 2 ≤ people ≤ type1 + type2; 1 ≤ type1, type2 ≤ 10^9.
Code runner format: three lines: people, type1, type2. Implement maxMinCandies(people, type1, type2); the harness prints X.
Clarifications
- Since every candy is handed out and nobody mixes types, each type goes to at least one person.