Show problems as
Classic problem #453MediumArrayMath

Minimum Moves to Equal Array Elements

Real-world version: Calibration Pulses To Level The Gauges
Tracked in this browser only
Read Minimum Moves to Equal Array Elements on LeetCode(opens in a new tab)

The 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.

Python
def min_moves(offsets: list[int]) -> int:
Java
public int minMoves(int[] offsets)