Frequency Problem (Easy Version) (Codeforces 1446D1)
Tags: Greedy, data structures

This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.You are given an array $$$[a_1, a_2, \dots, a_n]$$$. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs $$$f$$$ times in this subarray, then at least $$$2$$$ different values should occur exactly $$$f$$$ times.An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Input: The first line contains a single integer $$$n$$$ ($$$1 \le n \le 200\,000$$$) — the length of the array.The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le min(n, 100)$$$) — elements of the array.

Output: You should output exactly one integer  — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output $$$0$$$.

Note: In the first sample, the subarray $$$[1, 1, 2, 2, 3, 3]$$$ is good, but $$$[1, 1, 2, 2, 3, 3, 3]$$$ isn't: in the latter there are $$$3$$$ occurrences of number $$$3$$$, and no other element appears $$$3$$$ times.

Sample Inputs:
7
1 1 2 2 3 3 3

10
1 1 1 5 4 1 3 1 2 2

1
1

Sample Outputs:
6

7

0

Solution (Python):
import sys
from sys import stdin
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
lis = [0] * 101
for i in a:
    lis[i] += 1
f = 0
nmax = 1
for i in range(101):
    if lis[f] < lis[i]:
        f = i
        nmax = 1
    elif lis[f] == lis[i]:
        nmax += 1
if nmax > 1:
    print(n)
    sys.exit()
ans = 0
last = [0] * (2 * n)
lis = [None] * (2 * n)
for g in range(1, 101):
    if f == g:
        continue
    now = 0
    last[0] = g
    lis[0] = -1
    for i in range(n):
        if a[i] == f:
            now += 1
        elif a[i] == g:
            now -= 1
        if last[now] != g:
            last[now] = g
            lis[now] = i
        else:
            ans = max(ans, i - lis[now])
print(ans)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
cnt = [0] * 101
for x in a:
    cnt[x] += 1
maxFreq = max(cnt)
v = []
for i in range(101):
    if cnt[i] == maxFreq:
        v.append(i)
if len(v) > 1:
    print(n)
else:
    ans = 0
    for i in range(101):
        if i == v[0] or cnt[i] == 0:
            continue
        table = {0: -1}
        sum = 0
        for j in range(n):
            if a[j] == i:
                sum += 1
            if a[j] == v[0]:
                sum -= 1
            if sum in table:
                ind = table[sum]
                ans = max(ans, j - ind)
            else:
                table[sum] = j
    print(ans)

Solution (Python):
class UnionFindVerSize:

    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.group = N

    def find_root(self, x):
        if self._parent[x] == x:
            return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy:
            return
        self.group -= 1
        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)
import sys
from itertools import permutations
from heapq import heappop, heappush
from collections import deque
import random
import bisect
input = lambda : sys.stdin.readline().rstrip()
mi = lambda : map(int, input().split())
li = lambda : list(mi())

def solve():
    N = int(input())
    A = li()
    A = [a - 1 for a in A]
    M = max(A)
    cnt = [0] * (M + 1)
    for a in A:
        cnt[a] += 1
    max_cnt = max(cnt)
    if cnt.count(max_cnt) > 1:
        return N
    mode = cnt.index(max_cnt)
    res = 0
    cnt_to_mini = [N] * (2 * N + 1)
    cnt_to_maxi = [-1] * (2 * N + 1)
    for val in range(M + 1):
        if val == mode:
            continue
        cnt_to_mini[0] = cnt_to_maxi[0] = 0
        tmp = 0
        for i in range(N):
            if A[i] == mode:
                tmp += 1
            elif A[i] == val:
                tmp -= 1
            cnt_to_mini[tmp] = min(cnt_to_mini[tmp], i + 1)
            cnt_to_maxi[tmp] = max(cnt_to_maxi[tmp], i + 1)
        for i in range(-N, N + 1):
            res = max(res, cnt_to_maxi[i] - cnt_to_mini[i])
            (cnt_to_mini[i], cnt_to_maxi[i]) = (N, -1)
    return res
print(solve())