A facility operates n reservoirs, each currently holding some integer volume of water. During a single leveling round, the maintenance crew pours exactly one additional unit of water into every reservoir except one — the crew freely picks which reservoir to skip, and may pick a different one each round. Determine the minimum number of leveling rounds needed until every reservoir holds the same volume of water.
Print a single integer: the minimum number of leveling rounds needed until all reservoir volumes are equal.
Example 1
Input
3 1 2 3
Expected
3
Explanation
Starting from volumes [1,2,3], one valid sequence of rounds is [1,2,3] -> [2,3,3] -> [3,3,4] -> [4,4,4] (each round tops up two of the three reservoirs), taking 3 rounds — the minimum, since the reservoir starting at 1 needs 3 more units than the eventual common level relative to the others, matching sum(1,2,3) - 3*min = 6 - 3 = 3.
Example 2
Input
4 2 2 3 3
Expected
2
Explanation
Starting from [2,2,3,3], round 1 skips one of the reservoirs already at 3, giving [3,3,3,4]; round 2 skips the reservoir now at 4, giving [4,4,4,4]. That is 2 rounds, matching sum(2,2,3,3) - 4*min = 10 - 8 = 2.
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 →