Frequency of the Most Frequent Element - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,4], k = 5
Output: 3
💡 Note: We can make all elements equal to 4 by incrementing 1 by 3 and 2 by 2, using 5 operations total. Final array: [4,4,4] with frequency 3.
Example 2 — Limited Operations
$ Input: nums = [1,4,8,13], k = 5
Output: 2
💡 Note: We can make [1,4] both equal to 4 using 3 operations (1→4 needs 3 ops). Or make [4,8] both equal to 8 using 4 operations. Maximum frequency is 2.
Example 3 — Single Element
$ Input: nums = [3], k = 0
Output: 1
💡 Note: With only one element and no operations allowed, the maximum frequency is 1.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 105
  • 1 ≤ k ≤ 109

Visualization

Tap to expand
Maximum Frequency Problem: [1,2,4], k=5Original Array:124Target: Make all elements equal to 4+3 ops+2 ops+0 opsAfter Operations:444Total: 3+2+0 = 5 operations used, Frequency = 3
Understanding the Visualization
1
Input
Array [1,2,4] with k=5 operations available
2
Process
Choose target value and increment smaller elements
3
Output
Maximum achievable frequency is 3
Key Takeaway
🎯 Key Insight: Sort the array first, then use sliding window to find the longest subarray that can be made equal within k operations
Asked in
Amazon 45 Google 38 Microsoft 32 Apple 25
89.0K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen