#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, a[maxn], cnt[maxn], mx = 0, last[maxn * 2], ans = 0, vis[maxn];
void solve1(int x) {
  for (int i = 1; i <= n; i++) last[n - i] = last[n + i] = -1;
  last[n] = 0;
  int cur = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == x)
      cur++;
    else if (a[i] == mx)
      cur--;
    if (last[n + cur] == -1)
      last[n + cur] = i;
    else
      ans = max(ans, i - last[n + cur]);
  }
}
void solve2(int x) {
  for (int i = 1; i <= n; i++) cnt[i] = 0;
  int l = 1, equal = 0;
  cnt[a[l]]++;
  if (cnt[a[l]] == x) equal++;
  for (int i = 2; i <= n; i++) {
    cnt[a[i]]++;
    if (cnt[a[i]] == x)
      equal++;
    else if (cnt[a[i]] > x) {
      while (cnt[a[i]] > x) {
        cnt[a[l]]--;
        if (cnt[a[l]] == x - 1) equal--;
        l++;
      }
    }
    if (equal >= 2) ans = max(ans, i - l + 1);
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    mx = cnt[mx] < cnt[a[i]] ? a[i] : mx;
  }
  int limt = sqrt(n);
  for (int i = 1; i <= n; i++) {
    if (cnt[i] >= limt && i != mx && vis[i] == 0) {
      solve1(i);
      vis[i] = 1;
    }
  }
  for (int i = 1; i < limt; i++) solve2(i);
  cout << ans << endl;
}
