Minimum Increment to Make Array Unique

Medium Topics Company Tags

One move increases one entry of nums by 1. Return the fewest moves needed to make every entry distinct. The answer is guaranteed to fit in a signed 32-bit integer.

Example 1:

Input: nums = [0,0,0]

Output: 3

Explanation: Leave one 0 unchanged, increase another to 1, and the last to 2. This takes 0 + 1 + 2 = 3 moves, the minimum needed for three distinct nonnegative values.

Example 2:

Input: nums = [2,2,3]

Output: 2

Explanation: Increase one of the 2s to 4, leaving values [2,4,3], for two moves. One move would only create another duplicate value 3.

Example 3:

Input: nums = [5]

Output: 0

Explanation: The only entry is already unique, so no move is needed.

Constraints:

  • 1 <= nums.length <= 100000
  • 0 <= nums[i] <= 100000
  • The answer fits in a signed 32-bit integer.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.