Minimum Increment to Make Array Unique

You are given an integer array nums. In one move you pick an index i and increase nums[i] by 1. Return the minimum number of moves needed so that every value in nums is unique.

30:00

Minimum Increment to Make Array Unique
medium
Free written solution
Topics
Companies

You are given an integer array nums. In one move you pick an index i and increase nums[i] by 1. Return the minimum number of moves needed so that every value in nums is unique.

Example 1:
Input: {"nums":[1,2,2]}
Output: 1
Constraints:
  • 1≤nums.length≤1051 \leq \text{nums.length} \leq 10^5

  • 0≤nums[i]≤1050 \leq \text{nums}[i] \leq 10^5

  • The answer fits in a 32-bit integer.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,2]}

3 numbers. Values can only go up, so each duplicate must climb to a free value above it.

nums, input order

1

0

2

1

2

2

value line (one number per value)

need = 0

1

2

3

4

5

6

7

8

9

10

11

12

green: kept its value

amber: climbed (+increments)

moves so far

0

Variables
VariableValue
need0
moves0
DepthFunction Call
Stack empty
0/9