All problems
1072MediumArrayMathSorting

Bringing Every Gauge to One Reading

Tracked in this browser only
Write code

Trains the technique from

LeetCode 462Minimum Moves to Equal Array Elements II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def min_moves2(gauges: list[int]) -> int:
Java
public long minMoves2(int[] gauges)
September 7
Apply