#include <bits/stdc++.h>
using namespace std;
char _buf[100000], *_op(_buf), *_ed(_buf);
inline int getint() {
  int _s = 0, _f = 1;
  char _ch = (_op == _ed && (_ed = (_op = _buf) + fread(_buf, 1, 100000, stdin),
                             _op == _ed)
                  ? EOF
                  : *_op++);
  while (!('0' <= _ch && _ch <= '9'))
    (_ch == '-') && (_f = -1),
        _ch =
            (_op == _ed && (_ed = (_op = _buf) + fread(_buf, 1, 100000, stdin),
                            _op == _ed)
                 ? EOF
                 : *_op++);
  while (('0' <= _ch && _ch <= '9'))
    _s = _s * 10 + _ch - 48,
    _ch = (_op == _ed && (_ed = (_op = _buf) + fread(_buf, 1, 100000, stdin),
                          _op == _ed)
               ? EOF
               : *_op++);
  return _s * _f;
}
const int N = 2e5 + 1, mod = 998244353, INF = 0x3f3f3f3f;
inline void check(int& x) { x -= mod, x += x >> 31 & mod; }
inline void cmax(int& x, int y) {
  if (x - y >> 31) x = y;
}
inline void cmin(int& x, int y) {
  if (y - x >> 31) x = y;
}
int n, m, now, ans, a[N], c[N], s[N], at[101][N];
inline void input() {
  n = getint(), m = min(n, 100);
  for (int i = 1; i <= n; i++) a[i] = getint();
}
inline void init() {}
inline void clean() {
  for (int i = 1; i <= n; i++) c[i] = s[i] = 0;
  s[0] = n, now = 0;
}
inline void ins(int x) { s[c[x]++]--, s[c[x]]++, now += s[now + 1]; }
inline void work() {
  int l = 0, x;
  for (int i = 1; i <= n; i++) {
    ins(a[i]);
    if (s[now] >= 2) l = i;
  }
  for (int i = 1; i <= m; i++)
    if (c[i] == now) x = i;
  ans = l, clean();
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) at[i][j] = INF;
  for (int i = 1; i <= n; i++) {
    ins(a[i]);
    for (int j = 1; j <= m; j++)
      if (j != x && c[x] - c[j] >= 0) cmin(at[j][c[x] - c[j]], i);
    if (i > l) {
      for (int j = 1; j <= m; j++)
        if (j != x) cmax(ans, i - at[j][c[x] - c[j]]);
    }
  }
  cout << ans;
}
int main() {
  input();
  init();
  work();
  return 0;
}
