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).
Input: nums =[2,2,1,1,5,3,3,5]Output: 7Explanation: 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.
We want to find the longest prefix such that, after removing one element, all numbers have the same frequency. By tracking the count of each number and the count of each frequency, we can check at each step if the prefix is valid.
classSolution {
public:int maxEqualFreq(vector<int>& nums) {
unordered_map<int, int> cnt, freq;
int ans =0, n = nums.size();
for (int i =0; i < n; ++i) {
int x = nums[i];
if (cnt[x]) freq[cnt[x]]--;
cnt[x]++;
freq[cnt[x]]++;
int f1 = freq[1], f2 = freq[cnt[x]], maxf = cnt[x];
int total = i+1;
if (maxf ==1|| (freq[maxf]*maxf + freq[maxf-1]*(maxf-1) == total && freq[maxf] ==1) || (freq[1] ==1&& freq[maxf]*maxf + freq[maxf-1]*(maxf-1) == total-1))
ans = total;
}
return ans;
}
};
classSolution {
publicintmaxEqualFreq(int[] nums) {
Map<Integer, Integer> cnt =new HashMap<>(), freq =new HashMap<>();
int ans = 0;
for (int i = 0; i < nums.length; i++) {
int x = nums[i];
if (cnt.getOrDefault(x, 0) > 0) freq.put(cnt.get(x), freq.get(cnt.get(x))-1);
cnt.put(x, cnt.getOrDefault(x, 0)+1);
freq.put(cnt.get(x), freq.getOrDefault(cnt.get(x), 0)+1);
int maxf = cnt.get(x), total = i+1;
if (maxf == 1 || (freq.getOrDefault(maxf,0)*maxf + freq.getOrDefault(maxf-1,0)*(maxf-1) == total && freq.getOrDefault(maxf,0) == 1) || (freq.getOrDefault(1,0) == 1 && freq.getOrDefault(maxf,0)*maxf + freq.getOrDefault(maxf-1,0)*(maxf-1) == total-1))
ans = total;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funmaxEqualFreq(nums: IntArray): Int {
val cnt = mutableMapOf<Int, Int>()
val freq = mutableMapOf<Int, Int>()
var ans = 0for ((i, x) in nums.withIndex()) {
if (cnt[x] !=null&& cnt[x]!! > 0) freq[cnt[x]!!] = freq.getOrDefault(cnt[x]!!, 0) - 1 cnt[x] = cnt.getOrDefault(x, 0) + 1 freq[cnt[x]!!] = freq.getOrDefault(cnt[x]!!, 0) + 1val maxf = cnt[x]!!val total = i+1if (maxf ==1|| (freq.getOrDefault(maxf,0)*maxf + freq.getOrDefault(maxf-1,0)*(maxf-1) == total && freq.getOrDefault(maxf,0) ==1) || (freq.getOrDefault(1,0) ==1&& freq.getOrDefault(maxf,0)*maxf + freq.getOrDefault(maxf-1,0)*(maxf-1) == total-1))
ans = total
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defmaxEqualFreq(self, nums: list[int]) -> int:
from collections import defaultdict
cnt = defaultdict(int)
freq = defaultdict(int)
ans =0for i, x in enumerate(nums):
if cnt[x]:
freq[cnt[x]] -=1 cnt[x] +=1 freq[cnt[x]] +=1 maxf = cnt[x]
total = i+1if maxf ==1or \
(freq[maxf]*maxf + freq[maxf-1]*(maxf-1) == total and freq[maxf] ==1) or \
(freq[1] ==1and freq[maxf]*maxf + freq[maxf-1]*(maxf-1) == total-1):
ans = total
return ans