Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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].
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1090 <= k <= 109Problem summary: 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: Add an integer in the range [-k, k] to the element. Return the maximum possible number of distinct elements in nums after performing the operations.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,2,2,3,3,4] 2
[4,4,4,4] 1
least-number-of-unique-integers-after-k-removals)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3397: Maximum Number of Distinct Elements After Operations
class Solution {
public int maxDistinctElements(int[] nums, int k) {
Arrays.sort(nums);
int n = nums.length;
int ans = 0, pre = Integer.MIN_VALUE;
for (int x : nums) {
int cur = Math.min(x + k, Math.max(x - k, pre + 1));
if (cur > pre) {
++ans;
pre = cur;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3397: Maximum Number of Distinct Elements After Operations
func maxDistinctElements(nums []int, k int) (ans int) {
sort.Ints(nums)
pre := math.MinInt32
for _, x := range nums {
cur := min(x+k, max(x-k, pre+1))
if cur > pre {
ans++
pre = cur
}
}
return
}
# Accepted solution for LeetCode #3397: Maximum Number of Distinct Elements After Operations
class Solution:
def maxDistinctElements(self, nums: List[int], k: int) -> int:
nums.sort()
ans = 0
pre = -inf
for x in nums:
cur = min(x + k, max(x - k, pre + 1))
if cur > pre:
ans += 1
pre = cur
return ans
// Accepted solution for LeetCode #3397: Maximum Number of Distinct Elements After Operations
impl Solution {
pub fn max_distinct_elements(mut nums: Vec<i32>, k: i32) -> i32 {
nums.sort();
let mut ans = 0;
let mut pre = i32::MIN;
for &x in &nums {
let cur = (x + k).min((x - k).max(pre + 1));
if cur > pre {
ans += 1;
pre = cur;
}
}
ans
}
}
// Accepted solution for LeetCode #3397: Maximum Number of Distinct Elements After Operations
function maxDistinctElements(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
let [ans, pre] = [0, -Infinity];
for (const x of nums) {
const cur = Math.min(x + k, Math.max(x - k, pre + 1));
if (cur > pre) {
++ans;
pre = cur;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.