Frequency of the Most Frequent Element
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Sliding Window.
Frequency of the Most Frequent Element
Maximize the frequency of an element by incrementing other elements by at most
k overall.Visual Representation
nums = [1, 2, 4], k = 5
Sort: [1, 2, 4]
Window [1, 2, 4] ending at 4:
Cost to make all 4: (4-1) + (4-2) + (4-4) = 3 + 2 + 0 = 5
Cost 5 <= k (5)? YES! Frequency = 3Examples
Input: nums = [1,2,4], k = 5
Output: 3
Approach 1
Level I: Brute Force
Intuition
For each element
x in the array, try to make a sequence of elements equal to x ending at x. Use a sliding window to find the maximum possible frequency for each starting position.Thought Process
- Sort the array.
- Iterate
ifrom0ton-1. - Iterate
jfromiton-1. - Calculate
cost = sum(nums[j] - nums[k])forkin[i...j]. - If
cost <= k,maxFreq = max(maxFreq, j - i + 1).
⏱ O(N^2)💾 O(1)
Detailed Dry Run
| i | j | Window | Cost | Max Freq |
|---|---|---|---|---|
| 0 | 0 | [1] | 0 | 1 |
| 0 | 1 | [1, 2] | (2-1) = 1 | 2 |
| 0 | 2 | [1, 2, 4] | (4-1)+(4-2) = 5 | 3 |
Approach 2
Level II: Binary Search on Answer (O(N log N))
Intuition
After sorting, we can binary search for the maximum frequency
L. For a fixed L, we check if any subarray of length L exists such that the cost to make all elements equal to the largest element in that subarray is .Thought Process
- Sort the array.
- Search range for frequency
Lis[1, n]. - For each
midlength:- Use a fixed-size sliding window of size
mid. - Cost for window
[i, i + mid - 1]isnums[i + mid - 1] * mid - windowSum. - If any window has
cost <= k,midis possible.
- Use a fixed-size sliding window of size
- Adjust the search range accordingly.
Pattern: BS on Answer + Rolling Sum
⏱ O(N log N) - Sorting takes $O(N \log N)$, and binary search check takes $O(N)$ for each of the $\log N$ steps.💾 O(1) - Ignored sorting space.
Approach 3
Level III: Optimal (Sorting + Sliding Window)
Intuition
Thought Process
After sorting, for any window
[left, right], it's always optimal to make all elements equal to the largest element nums[right]. The total cost is nums[right] * windowSize - windowSum. We maintain the largest possible window that satisfies cost <= k.Patterns
- Sort then Window: Sorting allows monotonic cost calculations.
- Sum-based Cost:
cost = lastVal * len - sum.
⏱ O(N log N)💾 O(1)
Detailed Dry Run
| R | Num | Sum | Cost (4*Win - Sum) | Max Freq |
|---|---|---|---|---|
| 0 | 1 | 1 | 0 | 1 |
| 1 | 2 | 3 | 2*2 - 3 = 1 | 2 |
| 2 | 4 | 7 | 4*3 - 7 = 5 | 3 |
⚠️ Common Pitfalls & Tips
Integer overflow is a common issue here.
nums[right] * (right - left + 1) can exceed 2^31 - 1, so use 64-bit integers (long in Java/C++, default in Python/JS) for intermediate calculations.Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.