Talentd
FleetCode

1224. Maximum Equal Frequency

Hard38.8% acceptance1 company

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 <= 105
  • 1 <= nums[i] <= 105

Asked at 1 company

Hints open one at a time

Hint 1
Keep track of the min and max frequencies.
Hint 2
The number to be eliminated must have a frequency of 1, same as the others or the same +1.
7:54
Still stuck?Read 2 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxEqualFreq(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[2,2,1,1,5,3,3,5]
Input
[1,1,1,2,2,2,3,3,3,4,4,4,5]
Your code is saved on this device.Solution