#include <bits/stdc++.h>
template <class T>
void rd(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
using namespace std;
const int N = 2e5 + 10, M = 300;
int n, a[N], cnt[N], ans;
int tmp[N], ghb, ct;
int lst[N << 1];
void add(int x, int y) {
  ghb -= (tmp[x] == ct);
  tmp[x] += y;
  ghb += (tmp[x] == ct);
}
int main() {
  rd(n);
  for (int i = 1; i <= n; i++) rd(a[i]);
  int mx = 0, id = 0;
  for (int i = 1; i <= n; i++) {
    cnt[a[i]]++;
    if (cnt[a[i]] > mx) {
      mx = cnt[a[i]];
      id = a[i];
    }
  }
  for (ct = 1; ct <= M; ct++) {
    memset(tmp, 0, sizeof(tmp));
    ghb = 0;
    int l = 1, r = 1;
    while (r <= n) {
      add(a[r], 1);
      while (tmp[a[r]] > ct) add(a[l++], -1);
      if (ghb > 1) ans = max(ans, r - l + 1);
      r++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == id || cnt[i] <= M) continue;
    memset(lst, -1, sizeof(lst));
    int now = n + 1;
    lst[n + 1] = 0;
    for (int o = 1; o <= n; o++) {
      if (a[o] == id) now++;
      if (a[o] == i) now--;
      if (lst[now] != -1)
        ans = max(ans, o - lst[now]);
      else
        lst[now] = o;
    }
  }
  printf("%d\n", ans);
  return 0;
}
