Maximum Equal Frequency

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 is still considered that every appeared number has the same number of occurrences (0).

Example 1
Inputnums = [2,2,1,1,5,3,3,5]
Output7
For the prefix [2, 2, 1, 1, 5, 3, 3] of length 7, removing nums[4] = 5 leaves [2, 2, 1, 1, 3, 3], where each number appears exactly twice.
Example 2
Inputnums = [1,1,1,2,2,2,3,3,3,4,4,4,5]
Output13
The entire array of length 13 is a valid prefix because removing the single 5 leaves 1, 2, 3, and 4 each appearing exactly three times.

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate