Maximum Number of Distinct Elements After Operations - Problem

You're given an integer array nums and an integer k. Your mission is to maximize the number of distinct elements in the array by strategically adjusting each element.

The Rule: For each element in the array, you can perform at most one operation - add any integer from the range [-k, k] to that element. This means you can decrease it by up to k, increase it by up to k, or leave it unchanged.

Goal: Return the maximum possible number of distinct elements after performing these operations optimally.

Example: If nums = [1, 2, 2, 3] and k = 1, you could transform it to [1, 1, 3, 3] (no change, decrease by 1, increase by 1, no change) to get 3 distinct elements. But with better strategy: [0, 2, 1, 3] gives you 4 distinct elements!

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1, 2, 2, 3], k = 1
โ€บ Output: 4
๐Ÿ’ก Note: We can transform the array to [0, 1, 3, 4]: subtract 1 from first element (1-1=0), subtract 1 from second element (2-1=1), add 1 to third element (2+1=3), add 1 to fourth element (3+1=4). All four values are distinct.
example_2.py โ€” No Operations Needed
$ Input: nums = [4, 6, 16, 18], k = 3
โ€บ Output: 4
๐Ÿ’ก Note: All elements are already distinct and no operations are needed. The answer is 4.
example_3.py โ€” Limited by Range
$ Input: nums = [1, 1, 1, 1], k = 1
โ€บ Output: 3
๐Ÿ’ก Note: We can transform to [0, 1, 2, 1]: first element becomes 0 (1-1), second becomes 1 (no change), third becomes 2 (1+1), fourth stays as 1. But this gives duplicate! Better: [0, 1, 2, cannot assign unique value to fourth element within range [0,2]]. Maximum distinct is 3.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • 0 โ‰ค k โ‰ค 109
  • Each element can be modified at most once

Visualization

Tap to expand
Greedy Assignment VisualizationInput Array (sorted): [1, 2, 2, 3] k = 1Goal: Assign distinct values optimallyStrategy: Always choose the smallest available value within each element's rangeElement: 1Range: [0, 2]Assign: 0 โœ“Element: 2Range: [1, 3]Assign: 1 โœ“Element: 2Range: [1, 3]Assign: 2 โœ“Element: 3Range: [2, 4]Assign: 3 โœ“Next: 1Next: 2Next: 3Next: 4Result: [0, 1, 2, 3]4 Distinct Elements (Maximum Possible)Time: O(n log n) | Space: O(1)Key Insight: Greedy choice of smallest available value ensures optimal result!
Understanding the Visualization
1
Sort the array
Process elements in ascending order to handle smaller values first
2
Track next available value
Maintain a pointer to the next distinct value that can be assigned
3
Greedy assignment
For each element, assign max(next_available, element-k) if it's โ‰ค element+k
4
Update and continue
Increment next_available and move to the next element
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because choosing the smallest available value within range always leaves maximum flexibility for future assignments, guaranteeing the optimal solution.
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
26.8K Views
Medium Frequency
~18 min Avg. Time
834 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen