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.
Example 1:
Input: nums = [1,2,2] Output: 1 Explanation: After 1 move, the array could be [1, 2, 3].
Example 2:
Input: nums = [3,2,1,2,1,7] Output: 6 Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7]. It can be shown that it is impossible for the array to have all unique values with 5 or less moves.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for making all numbers in a list unique involves checking every possible increase we can make to the numbers. We try small adjustments and see if that fixes the problem; if not, we try bigger ones until all numbers are distinct.
Here's how the algorithm would work step-by-step:
def min_increment_for_unique(numbers):
total_increments = 0
for current_index in range(1, len(numbers)):
while True:
# Check against all previous numbers for uniqueness
is_unique = True
for previous_index in range(current_index):
if numbers[current_index] == numbers[previous_index]:
is_unique = False
break
if is_unique:
break
# Increment current number if not unique
numbers[current_index] += 1
total_increments += 1
return total_increments
To minimize increases, we need to think about grouping similar numbers together. The best way is to sort the numbers first, then we only need to increase a number if it's less than or equal to the number that came before it. We will increase the number until it is just one bigger than the previous number.
Here's how the algorithm would work step-by-step:
def minimum_increment_to_make_array_unique(numbers):
numbers.sort()
moves_needed = 0
previous_number = -1
for index in range(len(numbers)):
current_number = numbers[index]
# If the current number is less than or equal to
# the previous, we need to increment it.
if current_number <= previous_number:
moves_needed += previous_number - current_number + 1
numbers[index] = previous_number + 1
previous_number = numbers[index]
return moves_needed
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately as no increments are needed for an empty array. |
Array with a single element | Return 0 as a single element is inherently unique. |
Array with all identical elements | The algorithm should correctly increment these elements to achieve uniqueness. |
Array with a large range of values, potentially leading to large increments | Sorting and incrementing to the next available value will still work correctly, but consider potential integer overflow if the increments become excessively large. |
Array with negative numbers | The algorithm, if based on sorting and incrementing to next available element, should work correctly with negative numbers; the core logic isn't affected. |
Array with a few extremely large values and many small values | Sorting ensures the large values are handled last, avoiding unnecessary increments to smaller values that might later require even larger increments. |
Integer overflow when calculating the total increments | Use a 64-bit integer type (long in Java/C++, or appropriate type in Python) to store the sum of increments. |
Array is already unique | The algorithm should correctly return 0 as no increments are required. |