2968. Apply Operations to Maximize Frequency Score LeetCode Solution
In this guide, you will get 2968. Apply Operations to Maximize Frequency Score LeetCode Solution with the best time and space complexity. The solution to Apply Operations to Maximize Frequency Score problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Apply Operations to Maximize Frequency Score solution in C++
- Apply Operations to Maximize Frequency Score solution in Java
- Apply Operations to Maximize Frequency Score solution in Python
- Additional Resources

Problem Statement of Apply Operations to Maximize Frequency Score
You are given a 0-indexed integer array nums and an integer k.
You can perform the following operation on the array at most k times:
Choose any index i from the array and increase or decrease nums[i] by 1.
The score of the final array is the frequency of the most frequent element in the array.
Return the maximum score you can achieve.
The frequency of an element is the number of occurences of that element in the array.
Example 1:
Input: nums = [1,2,6,4], k = 3
Output: 3
Explanation: We can do the following operations on the array:
– Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
– Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
– Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
The element 2 is the most frequent in the final array so our score is 3.
It can be shown that we cannot achieve a better score.
Example 2:
Input: nums = [1,4,4,2,4], k = 0
Output: 3
Explanation: We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
0 <= k <= 1014
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
2968. Apply Operations to Maximize Frequency Score LeetCode Solution in C++
class Solution {
public:
int maxFrequencyScore(vector<int>& nums, long long k) {
int ans = 0;
long cost = 0;
ranges::sort(nums);
for (int l = 0, r = 0; r < nums.size(); ++r) {
cost += nums[r] - nums[(l + r) / 2];
while (cost > k)
cost -= nums[(l + r + 1) / 2] - nums[l++];
ans = max(ans, r - l + 1);
}
return ans;
}
};
/* code provided by PROGIEZ */
2968. Apply Operations to Maximize Frequency Score LeetCode Solution in Java
class Solution {
public int maxFrequencyScore(int[] nums, long k) {
int ans = 0;
long cost = 0;
Arrays.sort(nums);
// For a window [l, r], the best choice to make the numbers in the range
// equal is to make them all equal to the median in this range.
for (int l = 0, r = 0; r < nums.length; ++r) {
cost += nums[r] - nums[(l + r) / 2];
while (cost > k)
cost -= nums[(l + r + 1) / 2] - nums[l++];
ans = Math.max(ans, r - l + 1);
}
return ans;
}
}
// code provided by PROGIEZ
2968. Apply Operations to Maximize Frequency Score LeetCode Solution in Python
class Solution:
def maxFrequencyScore(self, nums: list[int], k: int) -> int:
nums.sort()
ans = 0
cost = 0
l = 0
for r, num in enumerate(nums):
cost += num - nums[(l + r) // 2]
while cost > k:
cost -= nums[(l + r + 1) // 2] - nums[l]
l += 1
ans = max(ans, r - l + 1)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.