#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int mod = 1e9 + 7;
const int N = 1e3 + 1;
const int L = 29;
const int bs = 300;
void solve() {
  int n;
  cin >> n;
  vector<int> arr(n);
  vector<int> freq(n + 1, 0);
  vector<vector<int>> pos(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    freq[arr[i]]++;
    pos[arr[i]].push_back(i);
  }
  int d = 1;
  for (int i = 1; i <= n; i++) {
    if (freq[i] > freq[d]) d = i;
  }
  for (int i = 1; i <= n; i++) {
    if (i != d && freq[i] == freq[d]) {
      cout << n << "\n";
      return;
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i == d) continue;
    if (freq[i] >= bs) {
      unordered_map<int, int> mp;
      mp[0] = -1;
      int curr = 0;
      for (int j = 0; j < n; j++) {
        if (arr[j] == d)
          curr++;
        else if (arr[j] == i)
          curr--;
        if (mp.count(curr)) {
          ans = max(ans, j - mp[curr]);
        } else {
          mp[curr] = j;
        }
      }
    } else {
      int ptr = 0;
      while (ptr < pos[i].size()) {
        int l = lower_bound(pos[d].begin(), pos[d].end(), pos[i][ptr]) -
                pos[d].begin();
        l -= min(l, freq[i] + 1);
        int ind = min(pos[i][ptr], pos[d][l]);
        unordered_map<int, int> mp;
        if (pos[i][ptr] < pos[d][l]) {
          ind = pos[i][ptr];
          if (ptr) {
            mp[0] = pos[i][ptr - 1] + 1;
          } else {
            mp[0] = -1;
          }
        } else {
          ind = pos[d][l];
          if (l) {
            mp[0] = pos[d][l - 1] + 1;
          } else {
            mp[0] = -1;
          }
        }
        int curr = 0;
        while (ind < n) {
          if (mp.count(curr)) {
            ans = max(ans, ind - 1 - mp[curr]);
          }
          if (arr[ind] == d)
            curr++;
          else if (arr[ind] == i)
            curr--;
          if (mp.count(curr)) {
            ans = max(ans, ind - mp[curr]);
          } else {
            mp[curr] = ind;
          }
          if (curr > 2 * freq[i]) break;
          int pd = lower_bound(pos[d].begin(), pos[d].end(), ind + 1) -
                   pos[d].begin();
          int pi = lower_bound(pos[i].begin(), pos[i].end(), ind + 1) -
                   pos[i].begin();
          if (pd == pos[d].size() && pi == pos[i].size()) {
            if (mp.count(curr)) {
              ans = max(ans, n - 1 - mp[curr]);
            }
            break;
          } else if (pd == pos[d].size()) {
            ptr = pi;
            ind = pos[i][pi];
          } else if (pi == pos[i].size()) {
            ind = pos[d][pd];
          } else {
            if (pos[d][pd] < pos[i][pi]) {
              ind = pos[d][pd];
            } else {
              ptr = pi;
              ind = pos[i][pi];
            }
          }
        }
        ptr++;
      }
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  std::cout << std::fixed << std::setprecision(20);
  ;
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
}
