Frequency of the Most Frequent Element

The frequency of an element is the number of times it occurs in an array.

You are given an integer array nums and an integer k. In one operation, you can choose an index of nums and increment the element at that index by 1.

Return the maximum possible frequency of an element after performing at most k operations.

Example 1
Inputnums = [1,2,4], k = 5
Output3
Increment the first element three times and the second element two times to make nums = [4, 4, 4], so 4 has a frequency of 3.
Example 2
Inputnums = [1,4,8,13], k = 5
Output2
There are multiple optimal solutions, each making one value appear twice using at most 5 operations.

Constraints

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

Asked at 11 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