#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
int n, B, a[200005], cnt[200005], c[200005], g, res;
int pre[200005], nxt[200005];
int dt[200005], p[200005];
int _tmp[200005 << 1], *lst = _tmp + 200005;
vector<int> v[200005];
void force(int x) {
  int u = 0;
  memset(_tmp, -1, sizeof _tmp);
  lst[0] = 0;
  for (register int i = (1); i <= (n); ++i) {
    if (a[i] == g) u++;
    if (a[i] == x) u--;
    if (lst[u] == -1)
      lst[u] = i;
    else
      res = max(res, i - lst[u]);
  }
}
signed main() {
  n = read();
  B = sqrt(n);
  for (register int i = (1); i <= (n); ++i) {
    a[i] = read(), cnt[a[i]]++;
    if (cnt[a[i]] > cnt[g]) g = a[i];
  }
  for (register int i = (1); i <= (n); ++i)
    if (i != g && cnt[i] > B) force(i);
  for (register int i = (1); i <= (B); ++i) {
    for (register int i = (1); i <= (n); ++i) c[i] = 0;
    int l = 1, all = 0;
    for (register int r = (1); r <= (n); ++r) {
      c[a[r]]++;
      if (c[a[r]] == i) all++;
      while (l <= r && c[a[r]] > i) {
        if (c[a[l]] == i) all--;
        c[a[l]]--, l++;
      }
      if (all >= 2) res = max(res, r - l + 1);
    }
  }
  cout << res;
  return 0;
}
