#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int N = 200001;
int a[N], c[N], mp[N * 2];
vector<int> li[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, mc = 0;
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    c[a[i]]++;
    li[a[i]].push_back(i);
    if (c[a[i]] > c[mc]) mc = a[i];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i != mc && c[i] == c[mc]) ans = n;
    if (c[i] < 200 || i == mc) continue;
    for (int j = 0; j < (n * 2 + 1); j++) mp[j] = n;
    int cs = n;
    mp[cs] = -1;
    for (int j = 0; j < (n); j++) {
      if (a[j] == mc)
        cs++;
      else if (a[j] == i)
        cs--;
      ans = max(ans, j - mp[cs]);
      if (mp[cs] == n) mp[cs] = j;
    }
  }
  li[mc].push_back(n);
  for (int i = 1; i <= n; i++) {
    if (c[i] >= 200 || !c[i] || i == mc) continue;
    ll cs = min(n, 2 * c[i] * (c[i] + 2));
    for (int j = 0; j < (cs * 2 + 1); j++) mp[j] = n;
    mp[cs] = -1;
    int lp = 0;
    for (int jj = 0; jj < (li[i].size()); jj++) {
      int j = li[i][jj];
      int pos =
          lower_bound((li[mc]).begin(), (li[mc]).end(), j) - li[mc].begin();
      lp = max(lp, pos - c[i] - 1);
      while (lp < li[mc].size() && li[mc][lp] < j) {
        cs++;
        if (mp[cs] == n) mp[cs] = li[mc][lp];
        lp++;
      }
      ans = max(ans, j - 1 - mp[cs]);
      cs--;
      if (mp[cs] == n) mp[cs] = j;
      while (lp < li[mc].size() &&
             (jj + 1 == li[i].size() || li[mc][lp] < li[i][jj + 1]) &&
             lp < pos + c[i] + 1) {
        ans = max(ans, li[mc][lp] - 1 - mp[cs]);
        cs++;
        if (mp[cs] == n) mp[cs] = li[mc][lp];
        lp++;
      }
    }
  }
  cout << ans;
}
