Skip to content
Discussion options

You must be logged in to vote

We need to find the maximum frequency of any element after performing operations where we can add values in the range [-k, k] to distinct elements.

Approach

The key insight is that for any target value x, the number of elements that can be transformed to x is:

  • Elements already equal to x
  • Plus elements within range [x-k, x+k] that can be transformed to x
  • But limited by the number of operations available

The solution works by:

  1. Finding the range of possible target values (from minV-k to maxV+k)
  2. Using a difference array to efficiently count how many elements can reach each possible target
  3. For each target value, calculating the maximum possible frequency considering both the elements that c…

Replies: 1 comment 2 replies

Comment options

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

kovatz Oct 21, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Oct 21, 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