LeetCode 1224 Maximum Equal Frequency: Counting & Case Analysis Solution
This article covers big tech's zero-tolerance policy on AI-assisted interview cheating and provides a complete solution to LeetCode 1224 Maximum Equal Frequency using frequency counting and case analysis with implementations in Java, C++, Python, and TypeScript.
Big Tech's Zero-Tolerance Policy on AI Interview Cheating
Recent discussions on Maimai reveal a surge in AI-assisted cheating during campus recruitment interviews. Candidates are observed glancing sideways or switching screens, and their code often passes all test cases on the first attempt. A big-tech employee reports that their department now enforces zero tolerance: any detected cheating results not only in immediate rejection but also a permanent record in the interview feedback system ( 面评). This record blocks the candidate from all business units within the company, effectively ending any future application chances. Previously, interviewers might simply fail a suspicious candidate without formal documentation; however, the volume of cheating has forced companies to raise the cost of violation.
LeetCode 1224: Maximum Equal Frequency
Problem Statement
Given a positive integer array nums, find the length of the longest prefix such that after deleting exactly one element from this prefix, every remaining number appears the same number of times. If the prefix becomes empty after deletion, it is considered valid (all numbers have frequency 0).
Example 1: nums = [2,2,1,1,5,3,3,5] → Output: 7. Explanation: The prefix [2,2,1,1,5,3,3] (length 7) becomes [2,2,1,1,3,3] after removing the 5 at index 4; each number appears twice.
Example 2: nums = [1,1,1,2,2,2,3,3,3,4,4,4,5] → Output: 13. The entire array satisfies the condition after removing the single 5.
Solution: Counting and Case Analysis
We maintain two arrays while scanning the array left to right: cnt[x] – frequency of value x in the current prefix. sum[f] – how many distinct values have frequency f. maxFreq – the maximum frequency seen so far.
For each new element t = nums[i] (prefix length len = i+1), we increment cnt[t] to cur, update sum[cur]++ and sum[cur-1]--, and update maxFreq. The prefix is valid if one of three conditions holds:
All elements are unique: maxFreq == 1. Deleting any element leaves all remaining frequencies equal to 1.
One element occurs once, all others occur maxFreq times: maxFreq * sum[maxFreq] + 1 == len. Removing the singleton balances the frequencies.
One element occurs maxFreq times, all others occur maxFreq-1 times: (maxFreq-1) * (sum[maxFreq-1] + 1) + 1 == len. Removing one occurrence from the max-frequency element makes all frequencies equal to maxFreq-1.
Whenever a condition is met, we update ans = len. The algorithm runs in O(n) time with O(M) space, where M is the maximum possible value (100010 in the implementation).
Code Implementations
Java
class Solution { static int[] cnt = new int[100010], sum = new int[100010]; public int maxEqualFreq(int[] nums) { Arrays.fill(cnt, 0); Arrays.fill(sum, 0); int n = nums.length, max = 0, ans = 0; for (int i = 0; i < n; i++) { int t = nums[i], cur = ++cnt[t], len = i + 1; sum[cur]++; sum[cur - 1]--; max = Math.max(max, cur); if (max == 1) ans = len; if (max * sum[max] + 1 == len) ans = len; if ((max - 1) * (sum[max - 1] + 1) + 1 == len) ans = len; } return ans; } }C++
class Solution { public: int maxEqualFreq(vector<int>& nums) { vector<int> cnt(100010, 0), sumv(100010, 0); int n = nums.size(), maxv = 0, ans = 0; for (int i = 0; i < n; i++) { int t = nums[i], cur = ++cnt[t], len = i + 1; sumv[cur]++; sumv[cur - 1]--; maxv = max(maxv, cur); if (maxv == 1) ans = len; if (maxv * sumv[maxv] + 1 == len) ans = len; if ((maxv - 1) * (sumv[maxv - 1] + 1) + 1 == len) ans = len; } return ans; } };Python
class Solution: def maxEqualFreq(self, nums: List[int]) -> int: cnt, sumv = [0] * 100010, [0] * 100010 n, maxv, ans = len(nums), 0, 0 for i, t in enumerate(nums): cur, lenv = cnt[t] + 1, i + 1 cnt[t] += 1 sumv[cur] += 1 sumv[cur - 1] -= 1 maxv = max(maxv, cur) if maxv == 1: ans = lenv if maxv * sumv[maxv] + 1 == lenv: ans = lenv if (maxv - 1) * (sumv[maxv - 1] + 1) + 1 == lenv: ans = lenv return ansTypeScript
function maxEqualFreq(nums: number[]): number { let n = nums.length, max = 0, ans = 0; const cnt = new Array<number>(100010).fill(0), sum = new Array<number>(100010).fill(0); for (let i = 0; i < n; i++) { let t = nums[i], len = i + 1, cur = ++cnt[t]; sum[cur]++; sum[cur - 1]--; max = Math.max(max, cur); if (max == 1) ans = len; if (max * sum[max] + 1 == len) ans = len; if ((max - 1) * (sum[max - 1] + 1) + 1 == len) ans = len; } return ans; }Complexity
Time Complexity: O(n) – single pass through the array.
Space Complexity: O(M) – fixed-size arrays of length 100010 (M = max element value).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
