Frequency of the Most Frequent Element - Problem

Imagine you have an array of numbers and a limited budget of operations to make elements more frequent. The frequency of an element is simply how many times it appears in the array.

You're given an integer array nums and an integer k representing your operation budget. In each operation, you can increment any element by 1. Your goal is to maximize the frequency of any single element after performing at most k operations.

Key insight: You can only increase elements (never decrease), so the optimal strategy involves picking a target value and incrementing smaller elements to match it.

Example: Given [1,2,4] and k=5, you could turn this into [4,4,4] using 5 operations: increment first element 3 times (1โ†’4) and second element 2 times (2โ†’4), achieving a frequency of 3.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,4], k = 5
โ€บ Output: 3
๐Ÿ’ก Note: We can increment 1 to 4 (3 operations) and 2 to 4 (2 operations) using exactly 5 operations. All three elements become 4, giving us a frequency of 3.
example_2.py โ€” Limited Operations
$ Input: nums = [1,4,8,13], k = 5
โ€บ Output: 2
๐Ÿ’ก Note: We can make 2 elements equal. For example, increment 1 to 4 (3 operations) or increment 4 to 8 (4 operations). With k=5, we can achieve frequency 2 but not 3.
example_3.py โ€” No Operations Needed
$ Input: nums = [3,9,6], k = 2
โ€บ Output: 1
๐Ÿ’ก Note: With only 2 operations, we cannot make any two elements equal. The minimum difference is |6-3|=3, which exceeds k=2. Maximum frequency remains 1.

Visualization

Tap to expand
Frequency Optimization VisualizationStep 1: Original Array124k = 5 operationsStep 2: After Sorting (No Change)124Step 3: Sliding Window AnalysisWindow [1,2,4] targeting 4:Cost = 3ร—4 - (1+2+4) = 5 โ‰ค k โœ“Valid window!Frequency = 3Step 4: Final Result444Max Frequency = 3Key Insightโ€ข Sort first to enable sliding windowโ€ข Window represents potential equal elementsโ€ข Cost formula: window_size ร— target - sumโ€ข Expand right, contract left as neededโ€ข Track maximum window size achievedTime: O(n log n), Space: O(1)
Understanding the Visualization
1
Sort Players by Score
Arrange players from lowest to highest score to identify efficient groupings
2
Use Sliding Window
Consider consecutive players as potential groups, expanding when budget allows
3
Calculate Boost Cost
For each group, calculate total boost points needed to equalize scores
4
Find Optimal Group Size
Track the largest group you can create within your boost point budget
Key Takeaway
๐ŸŽฏ Key Insight: Sort the array first, then use a sliding window where the window size represents how many elements can be made equal to the rightmost element within k operations. The cost formula efficiently calculates required operations without nested loops.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n elements as target, we potentially scan all n elements to calculate cost

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 105
  • 1 โ‰ค k โ‰ค 109
  • Important: You can only increment elements, never decrement
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
34.3K Views
High Frequency
~18 min Avg. Time
1.5K 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