Show problems as
Classic problem #462MediumArrayMathSorting

Minimum Moves to Equal Array Elements II

Real-world version: Bringing Every Gauge to One Reading
Tracked in this browser only
Read Minimum Moves to Equal Array Elements II on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A rail of gauges reads gauges. One turn changes a single gauge by one, up or down.

Return the fewest turns that leave every gauge on the same reading.

Examples

Example 1

Input
gauges = [1, 1, 1]
Output
0

Every gauge already reads the same, so nothing needs turning.

Example 2

Input
gauges = [1, 5]
Output
4

The two readings are four apart, and any reading between them costs exactly four turns altogether.

Example 3

Input
gauges = [4, 4, 4, 4, 10]
Output
6

Settling on four leaves the four gauges already there untouched and brings the ten down six turns. Settling anywhere higher would move four gauges instead of one.

Constraints

  • 1 <= gauges.length <= 10^5
  • -10^9 <= gauges[i] <= 10^9

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_moves2(gauges: list[int]) -> int:
Java
public long minMoves2(int[] gauges)