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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Given an array nums of positive integers, return the longest possible length of an array prefix of nums, such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it will have the same number of occurrences.
If after removing one element there are no remaining elements, it's still considered that every appeared number has the same number of ocurrences (0).
Example 1:
Input: nums = [2,2,1,1,5,3,3,5] Output: 7 Explanation: For the subarray [2,2,1,1,5,3,3] of length 7, if we remove nums[4] = 5, we will get [2,2,1,1,3,3], so that each number will appear exactly twice.
Example 2:
Input: nums = [1,1,1,2,2,2,3,3,3,4,4,4,5] Output: 13
Constraints:
2 <= nums.length <= 1051 <= nums[i] <= 105Problem summary: Given an array nums of positive integers, return the longest possible length of an array prefix of nums, such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it will have the same number of occurrences. If after removing one element there are no remaining elements, it's still considered that every appeared number has the same number of ocurrences (0).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[2,2,1,1,5,3,3,5]
[1,1,1,2,2,2,3,3,3,4,4,4,5]
remove-letter-to-equalize-frequency)count-submatrices-with-equal-frequency-of-x-and-y)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1224: Maximum Equal Frequency
class Solution {
private static int[] cnt = new int[100010];
private static int[] ccnt = new int[100010];
public int maxEqualFreq(int[] nums) {
Arrays.fill(cnt, 0);
Arrays.fill(ccnt, 0);
int ans = 0;
int mx = 0;
for (int i = 1; i <= nums.length; ++i) {
int v = nums[i - 1];
if (cnt[v] > 0) {
--ccnt[cnt[v]];
}
++cnt[v];
mx = Math.max(mx, cnt[v]);
++ccnt[cnt[v]];
if (mx == 1) {
ans = i;
} else if (ccnt[mx] * mx + ccnt[mx - 1] * (mx - 1) == i && ccnt[mx] == 1) {
ans = i;
} else if (ccnt[mx] * mx + 1 == i && ccnt[1] == 1) {
ans = i;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1224: Maximum Equal Frequency
func maxEqualFreq(nums []int) int {
cnt := map[int]int{}
ccnt := map[int]int{}
ans, mx := 0, 0
for i, v := range nums {
i++
if cnt[v] > 0 {
ccnt[cnt[v]]--
}
cnt[v]++
mx = max(mx, cnt[v])
ccnt[cnt[v]]++
if mx == 1 {
ans = i
} else if ccnt[mx]*mx+ccnt[mx-1]*(mx-1) == i && ccnt[mx] == 1 {
ans = i
} else if ccnt[mx]*mx+1 == i && ccnt[1] == 1 {
ans = i
}
}
return ans
}
# Accepted solution for LeetCode #1224: Maximum Equal Frequency
class Solution:
def maxEqualFreq(self, nums: List[int]) -> int:
cnt = Counter()
ccnt = Counter()
ans = mx = 0
for i, v in enumerate(nums, 1):
if v in cnt:
ccnt[cnt[v]] -= 1
cnt[v] += 1
mx = max(mx, cnt[v])
ccnt[cnt[v]] += 1
if mx == 1:
ans = i
elif ccnt[mx] * mx + ccnt[mx - 1] * (mx - 1) == i and ccnt[mx] == 1:
ans = i
elif ccnt[mx] * mx + 1 == i and ccnt[1] == 1:
ans = i
return ans
// Accepted solution for LeetCode #1224: Maximum Equal Frequency
struct Solution;
use std::collections::HashMap;
impl Solution {
fn max_equal_freq(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut res = 0;
let mut count: HashMap<i32, usize> = HashMap::new();
let mut freq: HashMap<usize, usize> = HashMap::new();
let mut max_freq = 0;
for i in 0..n {
let f = count.entry(nums[i]).or_default();
if *f != 0 {
*freq.entry(*f).or_default() -= 1;
}
*f += 1;
*freq.entry(*f).or_default() += 1;
max_freq = max_freq.max(*f);
if max_freq == 1
|| *freq.entry(max_freq).or_default() * max_freq == i
|| (*freq.entry(max_freq - 1).or_default() + 1) * (max_freq - 1) == i
{
res = res.max(i + 1);
}
}
res as i32
}
}
#[test]
fn test() {
let nums = vec![2, 2, 1, 1, 5, 3, 3, 5];
let res = 7;
assert_eq!(Solution::max_equal_freq(nums), res);
let nums = vec![1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5];
let res = 13;
assert_eq!(Solution::max_equal_freq(nums), res);
let nums = vec![1, 1, 1, 2, 2, 2];
let res = 5;
assert_eq!(Solution::max_equal_freq(nums), res);
}
// Accepted solution for LeetCode #1224: Maximum Equal Frequency
function maxEqualFreq(nums: number[]): number {
const n = nums.length;
const map = new Map();
for (const num of nums) {
map.set(num, (map.get(num) ?? 0) + 1);
}
for (let i = n - 1; i > 0; i--) {
for (const k of map.keys()) {
map.set(k, map.get(k) - 1);
let num = 0;
for (const v of map.values()) {
if (v !== 0) {
num = v;
break;
}
}
let isOk = true;
let sum = 1;
for (const v of map.values()) {
if (v !== 0 && v !== num) {
isOk = false;
break;
}
sum += v;
}
if (isOk) {
return sum;
}
map.set(k, map.get(k) + 1);
}
map.set(nums[i], map.get(nums[i]) - 1);
}
return 1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.