Frequency Problem (Easy Version) (Codeforces 1447F1)
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
input = sys.stdin.buffer.readline

def prog():
    n = int(input())
    a = list(map(int, input().split()))
    freq = [0] * 101
    for i in range(n):
        freq[a[i]] += 1
    mx = max(freq)
    amt = freq.count(mx)
    if amt >= 2:
        print(n)
    else:
        must_appear = freq.index(mx)
        ans = 0
        for j in range(1, 101):
            if j == must_appear:
                continue
            first_idx = [10 ** 6] * (n + 1)
            first_idx[0] = -1
            curr = 0
            for i in range(n):
                if a[i] == must_appear:
                    curr += 1
                elif a[i] == j:
                    curr -= 1
                ans = max(ans, i - first_idx[curr])
                first_idx[curr] = min(first_idx[curr], i)
        print(ans)
prog()

Solution (Python):
n = int(input())
arr = list(map(int, input().split()))
freq = [0] * 101
for i in arr:
    freq[i] += 1
maxx = max(freq)
amtOFmaxx = freq.count(maxx)
if amtOFmaxx >= 2:
    print(n)
else:
    must_apper = freq.index(maxx)
    ans = 0
    for j in range(1, 101):
        if j == must_apper:
            continue
        first_index = [10 ** 6] * (n + 1)
        first_index[0] = -1
        curr = 0
        for i in range(n):
            if arr[i] == must_apper:
                curr += 1
            elif arr[i] == j:
                curr -= 1
            ans = max(ans, i - first_index[curr])
            first_index[curr] = min(first_index[curr], i)
    print(ans)

Solution (Python):
""""""
from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, Counter, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
    return int(input())

def si():
    return input().rstrip()

def mi():
    return map(int, input().split())

def li():
    return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 998244353
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
    return ord(i) - ord('0')
file = 1

def solve():
    n = ii()
    a = li()
    b = Counter(a)
    (most_freq_elm, cnt) = (0, 1)
    for i in b:
        if b[i] > b[most_freq_elm]:
            most_freq_elm = i
            cnt = 1
        elif b[i] == b[most_freq_elm]:
            cnt += 1
    if cnt > 1:
        print(n)
        return
    ans = 0
    for i in b:
        if i == most_freq_elm:
            continue
        prev_occur = [-2] * (2 * n + 1)
        prev_occur[n] = -1
        cnt = 0
        for j in range(n):
            if a[j] == i:
                cnt += 1
            elif a[j] == most_freq_elm:
                cnt -= 1
            if prev_occur[cnt + n] == -2:
                prev_occur[cnt + n] = j
            else:
                ans = max(ans, j - prev_occur[cnt + n])
    print(ans)
if __name__ == '__main__':
    if file:
        if path.exists('input.txt'):
            sys.stdin = open('input.txt', 'r')
            sys.stdout = open('output.txt', 'w')
        else:
            input = sys.stdin.readline
    solve()