Apologies for the confusion! Here's the explanation in proper Markdown format with correct code blocks and proper formatting.
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: 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]
.
The frequency of 4
is 3.
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]
, where4
has a frequency of 2. - Increment the second element four times to make
nums = [1, 8, 8, 13]
, where8
has a frequency of 2. - Increment the third element five times to make
nums = [1, 4, 13, 13]
, where13
has a frequency of 2.
Input: nums = [3, 9, 6], k = 2
Output: 1
Explanation:
With k = 2
, it's impossible to increase any number enough times to make two elements the same. So the maximum frequency is 1.
- Sorting the array helps by grouping similar numbers together. Once sorted, we can efficiently increase the smaller numbers to match the larger ones.
- We use a sliding window technique to find the largest window of numbers that can be made identical using at most
k
operations. - If the number of operations exceeds
k
, we contract the window by moving the left pointer.
For any subarray [nums[l], nums[l+1], ..., nums[r]]
, we check if we can make all elements equal to nums[r]
by ensuring:
nums[r] * (r - l + 1) ≤ total + k
Where total
is the sum of the elements in the current window.
- Sort the array
nums
. - Use a sliding window with two pointers
left
andright
, both starting at0
, and atotal
to store the sum of the elements in the current window. - Expand the window by moving the
right
pointer. - If the current window violates the condition, shrink the window by moving the
left
pointer. - Track the maximum window size, which gives the maximum frequency.
class Solution {
public:
int maxFrequency(vector<int>& nums, int k) {
long long l = 0, r = 0, n = nums.size(), res = 0, total = 0;
sort(nums.begin(), nums.end());
while (r < n) {
total += nums[r];
while (nums[r] * (r - l + 1) > total + k) {
total -= nums[l];
l++;
}
res = max(res, (r - l + 1));
r++;
}
return res;
}
};
-
Sort the Input
Sorting helps group numbers together, making it easier to handle in the sliding window approach.sort(nums.begin(), nums.end());
-
Sliding Window
Use two pointersl
andr
to define the window. Calculate the total sum of elements in the window and use the formulanums[r] * (r - l + 1) > total + k
to check if we need to adjust the window.while (r < n) { total += nums[r]; while (nums[r] * (r - l + 1) > total + k) { total -= nums[l]; l++; } res = max(res, (r - l + 1)); r++; }
-
Track Maximum Frequency
Updateres
whenever the current window size (i.e.,r - l + 1
) is larger than the previous maximum.
-
Time Complexity:
Sorting the array takesO(n log n)
.
The sliding window technique takesO(n)
, so the overall time complexity isO(n log n)
. -
Space Complexity:
The space complexity isO(1)
(not counting the input space).
This sliding window approach efficiently solves the problem by dynamically adjusting the window size and ensuring that the total number of operations performed does not exceed k
.
If you found this helpful, don't forget to ⭐ star the repository and share it with others!