#include <bits/stdc++.h>
using namespace std;
template <typename G1, typename G2 = G1, typename G3 = G1>
struct triple {
  G1 first;
  G2 second;
  G3 T;
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n);
  for (auto &i : v) cin >> i, --i;
  int m = min(n, 100);
  vector<vector<int>> o(m);
  for (int i = 0; i < n; i++) o[v[i]].push_back(i);
  int mx = 0;
  for (int i = 0; i < m; i++)
    if (o[i].size() > o[mx].size()) mx = i;
  vector<int> mp(2 * n + 1, n);
  int ans = 0;
  for (int i = 0; i < m; i++)
    if (i != mx && !o[i].empty()) {
      if (o[i].size() == o[mx].size()) {
        ans = n;
        break;
      }
      int p1 = 0, p2 = 0, s = 0;
      mp[n] = -1;
      vector<int> upd;
      while (p1 < o[mx].size() || p2 < o[i].size()) {
        if (p1 == o[mx].size()) {
          ans = max(ans, o[i][p2] - mp[s + n] - 1);
          --s;
          mp[s + n] = min(mp[s + n], o[i][p2]);
          upd.push_back(s);
          ++p2;
        } else if (p2 == o[i].size()) {
          ans = max(ans, o[mx][p1] - mp[s + n] - 1);
          ++s;
          mp[s + n] = min(mp[s + n], o[mx][p1]);
          upd.push_back(s);
          ++p1;
        } else {
          if (o[mx][p1] > o[i][p2]) {
            ans = max(ans, o[i][p2] - mp[s + n] - 1);
            --s;
            mp[s + n] = min(mp[s + n], o[i][p2]);
            upd.push_back(s);
            ++p2;
          } else {
            ans = max(ans, o[mx][p1] - mp[s + n] - 1);
            ++s;
            mp[s + n] = min(mp[s + n], o[mx][p1]);
            upd.push_back(s);
            ++p1;
          }
        }
      }
      ans = max(ans, n - mp[s + n] - 1);
      for (auto j : upd) mp[j + n] = n;
    }
  cout << ans << '\n';
  return 0;
}
