#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 2e5 + 10;
int n, a[N], pos, cnt[N];
int pre[N << 1];
inline int calc(int x) {
  for (register int i = (0); i <= (2 * n); i++) pre[i] = -1;
  pre[n] = 0;
  int s = 0, ans = 0;
  for (register int i = (1); i <= (n); i++) {
    if (a[i] == pos) ++s;
    if (a[i] == x) --s;
    if (pre[s + n] == -1)
      pre[s + n] = i;
    else
      ans = max(ans, i - pre[s + n]);
  }
  return ans;
}
int main() {
  n = read();
  for (register int i = (1); i <= (n); i++) a[i] = read(), cnt[a[i]]++;
  int mx = 0, tot = 0;
  for (register int i = (1); i <= (n); i++) {
    if (cnt[i] > mx)
      mx = cnt[i], pos = i, tot = 1;
    else if (cnt[i] == mx)
      ++tot;
  }
  if (tot > 1) return printf("%d\n", n), 0;
  int ans = 0;
  for (register int i = (1); i <= (n); i++)
    if (cnt[i] && i != pos) ans = max(ans, calc(i));
  printf("%d\n", ans);
}
