#include <bits/stdc++.h>
using namespace std;
int a[200005], cnt[200005], mx = 0, d = 0, n, done[200005];
int f1(int freq) {
  vector<int> tmp(n + 1, 0);
  int i = 1, j = 1, eq = 0, ans = 0;
  while (i <= n) {
    tmp[a[i]]++;
    if (tmp[a[i]] == freq) eq++;
    while (tmp[a[i]] > freq) {
      tmp[a[j]]--;
      if (tmp[a[j]] == freq - 1) eq--;
      j++;
    }
    i++;
    if (eq >= 2) ans = max(ans, i - j);
  }
  return ans;
}
int f2(int v) {
  int p = 0, ans = 0;
  vector<int> first(2 * n + 2, -1);
  first[n] = 0;
  for (int i = 1; i <= n; i++) {
    p += (a[i] == d) - (a[i] == v);
    first[p + n] = first[p + n] == -1 ? i : first[p + n];
    ans = max(ans, i - first[p + n]);
  }
  return ans;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    if (cnt[a[i]] > mx) {
      mx = cnt[a[i]];
      d = a[i];
    }
  }
  int ans = 0;
  for (int i = 1; i < 200; i++) ans = max(ans, f1(i));
  for (int i = 1; i <= n; i++) {
    if (a[i] != d && cnt[a[i]] > 200 && !done[a[i]]) {
      done[a[i]] = 1;
      ans = max(ans, f2(a[i]));
    }
  }
  cout << ans << endl;
  return 0;
}
