Watch 10 video solutions for Frequency of the Most Frequent Element, a medium level problem involving Array, Binary Search, Greedy. This walkthrough by NeetCode has 123,885 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: nums = [1,2,4], k = 5 Output: 3 Explanation: Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.
Example 2:
Input: nums = [1,4,8,13], k = 5 Output: 2 Explanation: There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
Example 3:
Input: nums = [3,9,6], k = 2 Output: 1
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1051 <= k <= 105Problem Overview: You are given an integer array and an integer k. In one operation you can increment any element by 1. The goal is to maximize the frequency of some value after performing at most k increments. In other words, determine the largest group of equal numbers you can create by increasing smaller numbers.
Approach 1: Sliding Window Technique (O(n log n) time, O(1) space)
The key observation: if the array is sorted, you only need to raise smaller numbers to match a larger number on the right. After sorting, maintain a sliding window where the right pointer represents the target value all elements should match. Track the running sum of the window. The cost to convert all numbers in the window to nums[right] is nums[right] * window_size - window_sum. If that cost exceeds k, move the left pointer to shrink the window until the cost becomes valid again.
This works because sorting guarantees that increasing elements only moves them toward the current maximum. Each element enters and leaves the window once, so the window scan is linear. The only expensive step is sorting. This approach combines sorting with a classic sliding window pattern to efficiently track the largest feasible group.
Approach 2: Binary Search with Prefix Sums (O(n log n) time, O(n) space)
Another strategy treats the answer (maximum frequency) as a search space. After sorting the array, precompute a prefix sum array so you can quickly calculate the sum of any subarray. Then binary search on the possible frequency f. For each candidate frequency, check whether there exists a window of size f that can be converted to the same value using at most k increments.
To verify a window ending at index i, compute the cost to raise all elements in that window to nums[i]. Using prefix sums, the subarray sum is retrieved in O(1). The required operations become nums[i] * f - window_sum. If any window satisfies the k constraint, that frequency is feasible.
This approach explicitly searches the answer space using binary search. It is slightly more verbose than the sliding window solution but useful when you want a structured way to reason about feasibility problems.
Recommended for interviews: The sliding window approach is the expected solution. It shows you recognize that sorting converts the problem into expanding and shrinking a window based on operation cost. Binary search with prefix sums is also correct but usually considered a secondary method. Demonstrating the sliding window insight signals strong pattern recognition and optimization skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window with Sorting | O(n log n) | O(1) | Best general solution; efficient after sorting and commonly expected in interviews |
| Binary Search with Prefix Sums | O(n log n) | O(n) | Useful when treating frequency as a search space and validating ranges quickly |