Given an integer array nums, you may perform the following operation any number of times: choose an index i and increase nums[i] by 1.
Return the minimum number of operations required to make every value in nums distinct.
Constraints:
Input: nums = [0]-List[int]
Output: 0-int
Explanation: The array already has all distinct values, so no increments are needed.
Input: nums = [5, 1, 3, 0, 2]-List[int]
Output: 0-int
Explanation: All elements are already unique (0,1,2,3,5), so the minimum number of operations is 0.
Input: nums = [4, 4, 4, 4]-List[int]
Output: 6-int
Explanation: Sorting and greedily pushing duplicates forward, starting with [4,4,4,4], we keep the first 4, make the next 4 a 5 (1 op), the next a 6 (2 ops), and the last a 7 (3 ops), for a total of 1 + 2 + 3 = 6 operations; the final array could be [4,5,6,7].
Input: nums = [0, 1, 1, 2, 2, 2, 3]-List[int]
Output: 10-int
Explanation: After sorting (already sorted), each value must be at least 1 greater than the previous: 0 and 1 stay, the next 1 becomes 2 (1 op), the next 2 becomes 3 (1 op), the next 2 becomes 4 (2 ops), the next 2 becomes 5 (3 ops), and 3 becomes 6 (3 ops), for a total of 1 + 1 + 2 + 3 + 3 = 10 operations; the final array could be [0,1,2,3,4,5,6].
Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem: