 All Problems
Frequency of the Most Frequent Element
medium
sliding window
sorting
arrays
amazon
google

You can increment any element of an array at most k times (by 1 each). Return the maximum frequency of any element after performing at most k operations.

Example 1:

Input: 1 2 4
k: 5
Output: 3
Explanation: Increment 1 twice and 2 once to get [3,3,4] — wait, increment to 4: 1→4 (3 ops), 2→4 (2 ops) = 5 ops total, freq of 4 is 3.

Example 2:

Input: 1 4 8 13
k: 5
Output: 2

Constraints:

  • 1 ≤ nums.length ≤ 10⁵
  • 1 ≤ nums[i] ≤ 10⁵
  • 1 ≤ k ≤ 10⁵

Input format: First line: space-separated array. Second line: k.

Output format: Maximum frequency.

Run to check your code against the sample cases, or submit to run every case