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 a 0-indexed integer array nums and an integer k.
A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray.
Return the length of the longest possible equal subarray after deleting at most k elements from nums.
A subarray is a contiguous, possibly empty sequence of elements within an array.
Example 1:
Input: nums = [1,3,2,3,1,3], k = 3 Output: 3 Explanation: It's optimal to delete the elements at index 2 and index 4. After deleting them, nums becomes equal to [1, 3, 3, 3]. The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3. It can be proven that no longer equal subarrays can be created.
Example 2:
Input: nums = [1,1,2,2,1,1], k = 2 Output: 4 Explanation: It's optimal to delete the elements at index 2 and index 3. After deleting them, nums becomes equal to [1, 1, 1, 1]. The array itself is an equal subarray, so the answer is 4. It can be proven that no longer equal subarrays can be created.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= nums.length0 <= k <= nums.lengthProblem summary: You are given a 0-indexed integer array nums and an integer k. A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray. Return the length of the longest possible equal subarray after deleting at most k elements from nums. A subarray is a contiguous, possibly empty sequence of elements within an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Binary Search · Sliding Window
[1,3,2,3,1,3] 3
[1,1,2,2,1,1] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2831: Find the Longest Equal Subarray
class Solution {
public int longestEqualSubarray(List<Integer> nums, int k) {
Map<Integer, Integer> cnt = new HashMap<>();
int mx = 0, l = 0;
for (int r = 0; r < nums.size(); ++r) {
cnt.merge(nums.get(r), 1, Integer::sum);
mx = Math.max(mx, cnt.get(nums.get(r)));
if (r - l + 1 - mx > k) {
cnt.merge(nums.get(l++), -1, Integer::sum);
}
}
return mx;
}
}
// Accepted solution for LeetCode #2831: Find the Longest Equal Subarray
func longestEqualSubarray(nums []int, k int) int {
cnt := map[int]int{}
mx, l := 0, 0
for r, x := range nums {
cnt[x]++
mx = max(mx, cnt[x])
if r-l+1-mx > k {
cnt[nums[l]]--
l++
}
}
return mx
}
# Accepted solution for LeetCode #2831: Find the Longest Equal Subarray
class Solution:
def longestEqualSubarray(self, nums: List[int], k: int) -> int:
cnt = Counter()
l = 0
mx = 0
for r, x in enumerate(nums):
cnt[x] += 1
mx = max(mx, cnt[x])
if r - l + 1 - mx > k:
cnt[nums[l]] -= 1
l += 1
return mx
// Accepted solution for LeetCode #2831: Find the Longest Equal Subarray
// 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 #2831: Find the Longest Equal Subarray
// class Solution {
// public int longestEqualSubarray(List<Integer> nums, int k) {
// Map<Integer, Integer> cnt = new HashMap<>();
// int mx = 0, l = 0;
// for (int r = 0; r < nums.size(); ++r) {
// cnt.merge(nums.get(r), 1, Integer::sum);
// mx = Math.max(mx, cnt.get(nums.get(r)));
// if (r - l + 1 - mx > k) {
// cnt.merge(nums.get(l++), -1, Integer::sum);
// }
// }
// return mx;
// }
// }
// Accepted solution for LeetCode #2831: Find the Longest Equal Subarray
function longestEqualSubarray(nums: number[], k: number): number {
const cnt: Map<number, number> = new Map();
let mx = 0;
let l = 0;
for (let r = 0; r < nums.length; ++r) {
cnt.set(nums[r], (cnt.get(nums[r]) ?? 0) + 1);
mx = Math.max(mx, cnt.get(nums[r])!);
if (r - l + 1 - mx > k) {
cnt.set(nums[l], cnt.get(nums[l])! - 1);
++l;
}
}
return mx;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.