The frequency of an element is the number of times it occurs in an array.
You are given an integer array
Return the maximum possible frequency of an element after performing at most
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.
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.
Output: 1
Constraints:
1 <= nums.length <= 105 1 <= nums[i] <= 105 1 <= k <= 105
Contents
Solution 1 - Sort input array, then apply sliding window technique
In this approach, we are going to sort the input array first, then apply sliding window technique. The reason we will sort the array is because,
the right most element of the window should be either equal or greater than the left side elements, so that we know the value to what
we will make its left side elements to. For example, after sorting the input array is
Implementation steps:
-
First, we will sort the input array
nums . -
Then, create four variables
left ,right that we will use to adjust the window,sum to hold cumulative sum of elements in the current window, andmaxFrequency to hold the maximum frequency result. -
Next, run a
while loop untilright pointer reaches end of arraynums , and do the following:-
Add current element value to
sum . -
Then, check if the current window is going out of bounds, that is if we make all of the elements in the current window to match same as right element of the window.
(right - left + 1) * nums[right] > sum + k What we are doing here is, to match all left side elements in current window to same as current element (right indexed element), we need a value that should be either less than or equal towindow length * curent element , so we are checking it againstsum + k (cumulative sum of elements in current window andk values we can distribute among the elements in current window). So if thewindow length * curent element is greater thansum + k that means we are going out bounds and move the sliding window. So, to move the next window, run awhile loop until the above condition is true, and subtractleft pointer element value fromsum and incrementleft pointer.
For example, say if the array is[1, 2, 4, 9] , andk =2 , when we are at windowleft =0, right =2 sum = 1 + 2 + 4 = 7 . In order to make all of the current elements to 4, the total will be 12, but current sum is 7 and k=2, if we sum thesesum + k = 7 + 2 = 9 , so with k =2, we cannot make all current window elements to 4, so we have to move the slinding window to next position. -
Next, update the
maxFrequecy with maximum value betweencurrent window size and the currentmaxFrequency . -
At the end of while loop increment
right pointer to move to the next character.
-
Add current element value to
-
Finally, return
maxFrequency .
Complexity Analysis:
Time complexity: Above code runs in O(n log(n)) time, where
Space complexity: O(1), assumming that sorting will be done in-place algorithms like
Above implementations source code can be found at