Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an array nums of length n. You are also given an integer k.
You perform the following operation on nums once:
nums[i..j] where 0 <= i <= j <= n - 1.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 <= 1051 <= nums[i] <= 501 <= k <= 50Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Dynamic Programming · Greedy
[1,2,3,4,5,6] 1
[10,2,3,4,5,5,4,3,2,2] 10
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3434: Maximum Frequency After Subarray Operation
class Solution {
public int maxFrequency(int[] nums, int k) {
final int MAX = 50;
int maxFreq = 0;
for (int target = 1; target <= MAX; ++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;
}
}
// Accepted solution for LeetCode #3434: Maximum Frequency After Subarray Operation
package main
func maxFrequency(nums []int, k int) int {
f0, maxF12 := 0, 0
f1 := [51]int{}
for _, x := range nums {
if x == k {
maxF12++
f0++
} else {
f1[x] = max(f1[x], f0) + 1
maxF12 = max(maxF12, f1[x])
}
}
return maxF12
}
func maxFrequency2(nums []int, k int) int {
var f0, maxF1, f2 int
f1 := [51]int{}
for _, x := range nums {
f2 = max(f2, maxF1)
f1[x] = max(f1[x], f0) + 1
if x == k {
f2++
f0++
}
maxF1 = max(maxF1, f1[x])
}
return max(maxF1, f2)
}
func maxFrequency1(nums []int, k int) (ans int) {
set := map[int]struct{}{}
for _, x := range nums {
set[x] = struct{}{}
}
for target := range set {
var f0, f1, f2 int
for _, x := range nums {
f2 = max(f2, f1) + b2i(x == k)
f1 = max(f1, f0) + b2i(x == target)
f0 += b2i(x == k)
}
ans = max(ans, f1, f2)
}
return
}
func b2i(b bool) int {
if b {
return 1
}
return 0
}
# Accepted solution for LeetCode #3434: Maximum Frequency After Subarray Operation
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
// Accepted solution for LeetCode #3434: Maximum Frequency After Subarray Operation
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3434: Maximum Frequency After Subarray Operation
// class Solution {
// public int maxFrequency(int[] nums, int k) {
// final int MAX = 50;
// int maxFreq = 0;
// for (int target = 1; target <= MAX; ++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;
// }
// }
// Accepted solution for LeetCode #3434: Maximum Frequency After Subarray Operation
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3434: Maximum Frequency After Subarray Operation
// class Solution {
// public int maxFrequency(int[] nums, int k) {
// final int MAX = 50;
// int maxFreq = 0;
// for (int target = 1; target <= MAX; ++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;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.