3434. Maximum Frequency After Subarray Operation LeetCode Solution
In this guide, you will get 3434. Maximum Frequency After Subarray Operation LeetCode Solution with the best time and space complexity. The solution to Maximum Frequency After Subarray Operation 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
- Maximum Frequency After Subarray Operation solution in C++
- Maximum Frequency After Subarray Operation solution in Java
- Maximum Frequency After Subarray Operation solution in Python
- Additional Resources

Problem Statement of Maximum Frequency After Subarray Operation
You are given an array nums of length n. You are also given an integer k.
You perform the following operation on nums once:
Select a subarray nums[i..j] where 0 <= i <= j <= n – 1.
Select an integer x and add x to all the elements in nums[i..j].
Find the maximum frequency of the value k after the operation.
Example 1:
Input: nums = [1,2,3,4,5,6], k = 1
Output: 2
Explanation:
After adding -5 to nums[2..5], 1 has a frequency of 2 in [1, 2, -2, -1, 0, 1].
Example 2:
Input: nums = [10,2,3,4,5,5,4,3,2,2], k = 10
Output: 4
Explanation:
After adding 8 to nums[1..9], 10 has a frequency of 4 in [10, 10, 11, 12, 13, 13, 12, 11, 10, 10].
Constraints:
1 <= n == nums.length <= 105
1 <= nums[i] <= 50
1 <= k <= 50
Complexity Analysis
- Time Complexity: O(50n) = O(n)
- Space Complexity: O(n)
3434. Maximum Frequency After Subarray Operation LeetCode Solution in C++
class Solution {
public:
int maxFrequency(vector<int>& nums, int k) {
constexpr int kMax = 50;
int maxFreq = 0;
for (int target = 1; target <= kMax; ++target)
if (target != k)
maxFreq = max(maxFreq, kadane(nums, target, k));
return ranges::count(nums, k) + maxFreq;
}
private:
// Returns the maximum achievable frequency of `k` by Kakane's algorithm,
// where each `target` in subarrays is transformed to `k`.
int kadane(const vector<int>& nums, int target, int k) {
int maxSum = 0;
int sum = 0;
for (const int num : nums) {
if (num == target)
++sum;
else if (num == k)
--sum;
if (sum < 0) // Reset if sum becomes negative (Kadane's spirit).
sum = 0;
maxSum = max(maxSum, sum);
}
return maxSum;
}
};
/* code provided by PROGIEZ */
3434. Maximum Frequency After Subarray Operation LeetCode Solution in Java
class Solution {
public int maxFrequency(int[] nums, int k) {
final int kMax = 50;
int maxFreq = 0;
for (int target = 1; target <= kMax; ++target)
if (target != k)
maxFreq = Math.max(maxFreq, kadane(nums, target, k));
return (int) Arrays.stream(nums).filter(num -> num == k).count() + maxFreq;
}
// Returns the maximum achievable frequency of `k` using Kakane's algorithm,
// where each `target` in subarrays is transformed to `k`.
private int kadane(int[] nums, int target, int k) {
int maxSum = 0;
int sum = 0;
for (final int num : nums) {
if (num == target)
++sum;
else if (num == k)
--sum;
if (sum < 0) // Reset if sum becomes negative (Kadane's spirit).
sum = 0;
maxSum = Math.max(maxSum, sum);
}
return maxSum;
}
}
// code provided by PROGIEZ
3434. Maximum Frequency After Subarray Operation LeetCode Solution in Python
class Solution:
def maxFrequency(self, nums: list[int], k: int) -> int:
return nums.count(k) + max(self._kadane(nums, target, k)
for target in range(1, 51)
if target != k)
def _kadane(self, nums: list[int], target: int, k: int) -> int:
"""
Returns the maximum achievable frequency of `k` by Kakane's algorithm,
where each `target` in subarrays is transformed to `k`.
"""
maxSum = 0
sum = 0
for num in nums:
if num == target:
sum += 1
elif num == k:
sum -= 1
if sum < 0: # Reset sum if it becomes negative (Kadane's spirit).
sum = 0
maxSum = max(maxSum, sum)
return maxSum
# 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.