A logistics coordinator has a list of shipment weights and a shortlist of candidate shipping-container capacities. For a candidate capacity, its divisibility score is the number of shipments whose weight is an exact multiple of that capacity, meaning the shipment can be loaded into containers of that capacity with no wasted space. Find the candidate capacity with the strictly highest divisibility score; if two or more candidates tie for the highest score, report the smallest capacity value among them (if that value itself appears more than once in the candidate list, it is still reported only once).
Print a single integer: the candidate capacity with the maximum divisibility score, breaking ties by the smallest capacity value.
Example 1
Input
4 3 4 8 6 12 2 3 4
Expected
2
Explanation
Shipment weights are [4, 8, 6, 12] and candidate capacities are [2, 3, 4]. Capacity 2 divides all four weights evenly, for a score of 4. Capacity 3 only divides 6 and 12, for a score of 2. Capacity 4 divides 4, 8 and 12, for a score of 3. Capacity 2 has the highest score, so the answer is 2.
Example 2
Input
3 3 10 20 30 5 10 15
Expected
5
Explanation
Shipment weights are [10, 20, 30] and candidate capacities are [5, 10, 15]. Capacity 5 divides all three weights (score 3), capacity 10 also divides all three weights (score 3), and capacity 15 only divides 30 (score 1). Capacities 5 and 10 tie for the highest score of 3, so the smaller value, 5, is reported.
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 →