Apply Operations to Maximize Frequency Score

You are given a 0-indexed integer array nums and an integer k.

You can perform the following operation on the array at most k times:

  • Choose any index i from the array and increase or decrease nums[i] by 1.

The score of the final array is the frequency of the most frequent element in the array.

Return the maximum score you can achieve.

The frequency of an element is the number of occurences of that element in the array.

Example 1
Inputnums = [1,2,6,4], k = 3
Output3
By changing values to make three elements equal to 2, the most frequent element has frequency 3, and no better score is possible.
Example 2
Inputnums = [1,4,4,2,4], k = 0
Output3
No operations can be applied, so the score is the frequency of the most frequent element in the original array, which is 3.

Constraints

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

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