Minimum Moves to Equal Array Elements
Real-world version: Calibration Pulses To Level The GaugesThe statement here is our own: the same inputs, outputs and technique.
A rack carries n pressure gauges, and offsets[i] is the signed zero-offset of gauge i measured in ticks. An offset can sit below zero.
A pulse singles out one gauge and lifts the offset of every gauge except that one by exactly one tick. The singled-out gauge is untouched.
Return the fewest pulses that leave all n offsets reading the same value. The answer always fits in a signed 32-bit integer.
Examples
Example 1
- Input
- offsets = [4,7,9]
- Output
- 8
Eight pulses can settle the rack at 12: gauge 1 is the singled-out gauge on 3 of them, gauge 2 on the other 5, and gauge 0 on none. Gauge 0 is lifted all 8 times to 12, gauge 1 is lifted 5 times to 12, and gauge 2 is lifted 3 times to 12.
Example 2
- Input
- offsets = [-3,-3]
- Output
- 0
Both offsets already read the same value, so no pulse is needed.
Example 3
- Input
- offsets = [-5,0,2,2]
- Output
- 19
Nineteen pulses can settle the rack at 14, with gauge 0 singled out on none of them, gauge 1 on 5 of them, and gauges 2 and 3 on 7 each. Offsets may start below zero, as gauge 0 does here.
Example 4
- Input
- offsets = [6]
- Output
- 0
A rack of one gauge is already uniform, so the count is zero.
Example 5
- Input
- offsets = [8,3]
- Output
- 5
Five pulses can settle the rack at 8, with gauge 0 singled out on every one of them, so only gauge 1 is ever lifted.
Constraints
- n == offsets.length
- 1 <= n <= 10^5
- -10^9 <= offsets[i] <= 10^9
- The answer is guaranteed to fit in a signed 32-bit integer.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def min_moves(offsets: list[int]) -> int:public int minMoves(int[] offsets)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 5 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's