The primary thought was to sort the array and then iterate through it. If an element is found to be equal to or less than its previous element, increment it to make it unique while keeping track of the total number of increments needed.
- Sort the array
nums
. - Initialize a variable
tracker
to keep track of the total increments needed. - Iterate through the sorted array starting from the second element.
- If the current element is less than or equal to the previous element, calculate the difference between them and increment the current element to make it unique. Add the required number of increments to the
tracker
. - Continue this process until the end of the array is reached.
- Return the value of
tracker
as the result.
- Time complexity: O(n log n), where n is the number of elements in the array. This is due to the sorting operation.
- Space complexity: O(1), as the space used is independent of the input size, except for the input itself.