#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const int bit = 32;
const double pi = acos(-1.0);
int d[M * 2], a[M];
int n, sum[M];
int ca(int x, int ma) {
  for (int i = 0; i <= 2 * n; i++) {
    d[i] = -1;
  }
  d[sum[ma]] = 0;
  int cnt = 0, ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == x) cnt++;
    if (a[i] == ma) cnt--;
    if (d[sum[ma] + cnt] != -1)
      ans = max(ans, i - d[sum[ma] + cnt]);
    else
      d[sum[ma] + cnt] = i;
  }
  return ans;
}
int cd(int x) {
  int l = 1;
  for (int i = 1; i <= n; i++) d[i] = 0;
  d[a[l]]++;
  int cnt = 0;
  if (d[a[l]] == x) cnt++;
  int ans = 0;
  for (int i = 2; i <= n; i++) {
    d[a[i]]++;
    if (d[a[i]] == x)
      cnt++;
    else if (d[a[i]] > x) {
      while (d[a[i]] > x) {
        d[a[l]]--;
        if (d[a[l]] == x - 1) cnt--;
        l++;
      }
    }
    if (cnt >= 2) ans = max(ans, i - l + 1);
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  int p = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (++sum[a[i]] >= sum[p]) p = a[i];
  }
  if (sum[p] == n) {
    puts("0");
    return 0;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (sum[i] > 200 && i != p) {
      ans = max(ans, ca(i, p));
    }
  }
  for (int i = 1; i <= 200; i++) {
    ans = max(ans, cd(i));
  }
  printf("%d\n", ans);
  return 0;
}
