You are given an integer array nums
and an integer k
.
You are allowed to perform the following operation on each element of the array at most once:
[-k, k]
to the element.Return the maximum possible number of distinct elements in nums
after performing the operations.
Example 1:
Input: nums = [1,2,2,3,3,4], k = 2
Output: 6
Explanation:
nums
changes to [-1, 0, 1, 2, 3, 4]
after performing operations on the first four elements.
Example 2:
Input: nums = [4,4,4,4], k = 1
Output: 3
Explanation:
By adding -1 to nums[0]
and 1 to nums[1]
, nums
changes to [3, 5, 4, 4]
.
from collections import Counter
def max_distinct_elements(nums, k):
"""
Calculates the maximum possible number of distinct elements in nums after performing operations.
Args:
nums (List[int]): The input integer array.
k (int): The maximum allowed change to each element.
Returns:
int: The maximum number of distinct elements after operations.
"""
counts = Counter(nums)
distinct_count = len(counts)
duplicates = []
for num, count in counts.items():
if count > 1:
duplicates.append(count - 1)
duplicates.sort()
for i in range(len(duplicates)):
if k >= duplicates[i]:
k -= duplicates[i]
distinct_count += 1
else:
distinct_count += k
k = 0
break
distinct_count += min(k, len(nums) - distinct_count)
return distinct_count
# Example usage:
nums1 = [1, 2, 2, 3, 3, 4]
k1 = 2
print(f"Example 1: nums = {nums1}, k = {k1}, Output = {max_distinct_elements(nums1, k1)}") # Output: 6
nums2 = [4, 4, 4, 4]
k2 = 1
print(f"Example 2: nums = {nums2}, k = {k2}, Output = {max_distinct_elements(nums2, k2)}") # Output: 3
nums3 = [1,1,3,3,3]
k3 = 2
print(f"Example 3: nums = {nums3}, k = {k3}, Output = {max_distinct_elements(nums3, k3)}") # Output: 4
nums4 = [1,10,3,3,3,4,4]
k4 = 3
print(f"Example 4: nums = {nums4}, k = {k4}, Output = {max_distinct_elements(nums4, k4)}") # Output: 7
nums5 = [4,1,5,4,1,5]
k5 = 2
print(f"Example 5: nums = {nums5}, k = {k5}, Output = {max_distinct_elements(nums5, k5)}") # Output: 6
nums6 = [2,2,5,7,7,7,9,9,10,10]
k6 = 2
print(f"Example 6: nums = {nums6}, k = {k6}, Output = {max_distinct_elements(nums6, k6)}") # Output: 9
Counter
to count the frequency of each number in the input array nums
. This helps identify how many duplicates exist for each number.duplicates
. Sort this list to process elements with fewer duplicates first.duplicates
list. For each duplicate count, check if k
is sufficient to eliminate the duplicates. If yes, decrement k
by the number of duplicates and increment the distinct_count
. Otherwise, add k
to the distinct_count
and set k
to 0, breaking the loop.k
, it means we can potentially make more elements distinct. We add the minimum of k
and the number of non-distinct elements to the distinct_count
. The number of non-distinct elements is calculated as len(nums) - distinct_count
.distinct_count
.Time Complexity: O(N log N), where N is the length of the input array nums
. This is due to the sorting of the duplicates
list. The rest of the operations (counting frequencies, iterating through counts and duplicates) take O(N) time.
Space Complexity: O(N), where N is the length of the input array nums
. This is because the Counter
can store up to N distinct elements, and the duplicates
list can also contain up to N elements in the worst case.
nums
is empty, the function should return 0. This case is implicitly handled as the loop will not execute.k
is 0, the function should return the number of distinct elements in the original array. This is handled correctly as no duplicates can be removed, and no additional distinct elements can be created.min
part is important when k is large. E.g. nums = [1,1,1,1], k = 5, Output should be 4, not 6.k
is very large, it should not result in more than len(nums)
distinct elements. This condition is handled correctly in the final step where distinct_count += min(k, len(nums) - distinct_count)
.