All problems
1103MediumArrayGreedySortingCounting

Nudging Dials Apart

Tracked in this browser only
Write code

Trains the technique from

LeetCode 945Minimum Increment to Make Array Unique

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 row of dials shows whole-number readings, and several dials may show the same reading. One nudge picks a single dial and raises its reading by 1.

Return the fewest nudges that leave no two dials showing the same reading.

Examples

Example 1

Input
dials = [0, 0]
Output
1

One of the two dials climbs to 1, which takes a single nudge.

Example 2

Input
dials = [2, 2, 2, 2, 2]
Output
10

Leave one dial where it is and lift the other four to 3, 4, 5 and 6, costing 1 plus 2 plus 3 plus 4.

Example 3

Input
dials = [1, 2, 3, 4, 5]
Output
0

No two dials already share a reading, so nothing has to move.

Constraints

  • 1 <= dials.length <= 10^5
  • 0 <= dials[i] <= 10^5

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_increment_for_unique(dials: list[int]) -> int:
Java
public int minIncrementForUnique(int[] dials)
September 7
Apply