Skip to content
Discussion options

You must be logged in to vote

We need to find the maximum number of distinct elements after being allowed to add any integer in the range [-k, k] to each element at most once.

The key insight is that for each number, we can transform it to any value in the range [num-k, num+k]. To maximize distinct elements, we should assign the smallest possible distinct value to each number while ensuring it stays within its allowed range.

Approach:

  1. Sort the array to process numbers in order
  2. For each number, try to assign the smallest possible value that:
    • Is greater than the previously assigned value
    • Is within the number's allowed range [num-k, num+k]
  3. Keep track of the last assigned value and count how many distinct values we ca…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Oct 18, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Oct 18, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants