Maximum Frequency of an Element After Performing Operations II

You are given an integer array nums and two integers k and numOperations.

You must perform an operation numOperations times on nums, where in each operation you:

  • Select an index i that was not selected in any previous operations.
  • Add an integer in the range [-k, k] to nums[i].

Return the maximum possible frequency of any element in nums after performing the operations.

Example 1
Inputnums = [1,4,5], k = 1, num_operations = 2
Output2
Adding 0 to nums[1] and -1 to nums[2] makes nums become [1, 4, 4], giving a maximum frequency of 2.
Example 2
Inputnums = [5,11,20,20], k = 5, num_operations = 1
Output2
Adding 0 to nums[1] still leaves the value 20 appearing twice, giving a maximum frequency of 2.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 0 <= k <= 10^9
  • 0 <= numOperations <= nums.length

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate