Apply Operations to Maximize Frequency Score - Problem

Imagine you're a data scientist with a powerful tool: you can modify any number in your dataset by increasing or decreasing it by 1, but you have a limited budget of k operations.

Given a 0-indexed integer array nums and an integer k, your goal is to strategically apply these operations to maximize the frequency of the most common element in the final array.

The Challenge:

  • You can perform at most k operations
  • Each operation allows you to +1 or -1 any element
  • Your score is the frequency (count) of the most frequent element
  • Return the maximum possible score

Example: With nums = [1,2,6,4] and k = 3, you could transform it to [1,2,2,2] using 3 operations: 6→4→3→2, giving you a frequency score of 3.

Input & Output

example_1.py — Basic Example
$ Input: nums = [1,2,6,4], k = 3
Output: 3
💡 Note: We can transform the array to [2,2,2,4] by changing 1→2 (1 operation) and 6→4→3→2 (3 operations). This gives us 3 occurrences of the value 2, but we've used 4 operations total. Better: transform to [1,1,4,4] using 1 operation (2→1) and 2 operations (6→5→4), achieving frequency 2 with 3 operations. Actually optimal: [2,2,2,6] using 1+0+2=3 operations for frequency 3.
example_2.py — Small Budget
$ Input: nums = [1,4,4], k = 1
Output: 2
💡 Note: We already have two 4's. With 1 operation, we could make 1→4 to get three 4's, but that costs 3 operations. We could make 1→2 or 1→3, but that doesn't help. We can achieve frequency 2 by doing nothing (the two 4's already exist) using 0 operations ≤ k=1.
example_3.py — Edge Case
$ Input: nums = [1], k = 0
Output: 1
💡 Note: Single element array always has maximum frequency of 1, regardless of k value.

Visualization

Tap to expand
The Sculptor's WorkshopOriginal pieces:1264After sorting:1246Sliding Window AnalysisWindow size = 3: [1,2,4]Target = 2: Cost = 1+0+2 = 3 ≤ k✓ Frequency 3 is achievable!Try frequency 4: [1,2,4,6]Target = 2.5 → 2: Cost = 1+0+2+4 = 7 > k✗ Frequency 4 not achievableBest Group3Maximum Frequency
Understanding the Visualization
1
Arrange by Size
Sort the numbers like arranging clay pieces by height
2
Choose Group Size
Binary search to find the largest group size possible
3
Test Windows
For each group size, slide a window across sorted array
4
Calculate Cost
Find minimum cost to make each window identical
Key Takeaway
🎯 Key Insight: Sort first to group similar values, then use binary search + sliding window to efficiently find the largest achievable frequency within the operation budget.

Time & Space Complexity

Time Complexity
⏱️
O(n² log n)

O(n log n) for sorting, O(log n) for binary search, O(n²) for sliding window validation

n
2n
Quadratic Growth
Space Complexity
O(n)

Space for sorted array and prefix sums

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 0 ≤ k ≤ 2 × 1014
  • All values fit in standard integer types
Asked in
Google 42 Meta 38 Amazon 31 Microsoft 24
62.1K Views
Medium-High Frequency
~35 min Avg. Time
1.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