A regional distribution center has n pallets waiting to ship, each holding a known number of boxes. The center must choose a single container capacity — a positive integer number of boxes that every container it uses can hold — and then pack each pallet's boxes into that many containers, where a pallet needing a fractional container still requires one additional whole container to hold the remainder (so a pallet of 9 boxes packed into containers of capacity 5 needs 2 containers). The loading dock can process at most quota containers per day, counting containers from all pallets together. Find the smallest container capacity for which the total number of containers needed across all pallets does not exceed quota. It is guaranteed that at least one valid capacity exists.
A single integer: the smallest valid container capacity.
Example 1
Input
4 6 1 2 5 9
Expected
5
Explanation
With capacity 5, the pallets need ceil(1/5)+ceil(2/5)+ceil(5/5)+ceil(9/5) = 1+1+1+2 = 5 containers, which meets the quota of 6. Capacity 4 would need 1+1+2+3 = 7 containers, exceeding the quota, so 5 is the smallest valid capacity.
Example 2
Input
5 5 44 22 33 11 1
Expected
44
Explanation
The quota equals the number of pallets (5), so every pallet must fit into exactly one container, meaning the capacity must be at least the largest pallet's box count, 44. Capacity 44 gives exactly 5 containers total, meeting the quota exactly, so 44 is the minimum.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →