#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, K = 300;
int n, mx, ct, ans, a[N], buc[N], lst[N * 2], tp[N], cnt;
inline void Add(int x, int k) {
  cnt -= (tp[x] == ct), tp[x] += k, cnt += (tp[x] == ct);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), ++buc[a[i]];
  mx = max_element(buc + 1, buc + n + 1) - buc;
  for (ct = 1; ct <= K; ++ct) {
    memset(tp, 0, sizeof(tp)), cnt = 0;
    for (int l = 1, r = 1; r <= n; ++r) {
      Add(a[r], 1);
      while (tp[a[r]] > ct) Add(a[l++], -1);
      if (cnt > 1) ans = max(ans, r - l + 1);
    }
  }
  for (int i = 1, s; i <= n; ++i)
    if (i != mx && buc[i] > K) {
      memset(lst, -1, sizeof(lst)), lst[s = n] = 0;
      for (int j = 1; j <= n; ++j) {
        s += a[j] == mx ? 1 : (a[j] == i ? -1 : 0);
        if (~lst[s])
          ans = max(ans, j - lst[s]);
        else
          lst[s] = j;
      }
    }
  printf("%d", ans);
  return 0;
}
