Skip to content

Practice Minimum Increment to Make Array Unique

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#945
LeetCode Problem

Minimum Increment to Make Array Unique

Problem Statement

You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1. Return the minimum number of moves to make every value in nums unique. The test cases are generated so that the answer fits in a 32-bit integer.

Source: Minimum Increment to Make Array Unique on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,2]
Output
1
Explanation

Increment one of the two 2s to 3. The resulting values are [1,2,3], so one move is enough. Zero moves cannot work because the original array contains a duplicate. When processing sorted values, keep each value if it is greater than the previous assigned value; otherwise raise it to that previous value plus one. Raising it further would spend extra moves without helping any later value.

# Example 2

Input
nums = [3,2,1,2,1,7]
Output
6
Explanation

Sort the values to reason about their minimum destinations: [1,1,2,2,3,7]. Assign them the distinct values [1,2,3,4,5,7], costing 0 + 1 + 1 + 2 + 2 + 0 = 6 increments. Each destination is the smallest value that is both at least the original value and larger than the preceding destination. This gives a lower bound that the assignment achieves. Simply counting duplicate occurrences would miss the extra increments caused by collisions with 3 and other newly assigned values.

Constraints

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

Related LeetCode Problems

Practice more problems similar to Minimum Increment to Make Array Unique.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.