#include <bits/stdc++.h>
const int maxn = 2e5 + 9, inf = 0x3f3f3f3f;
void Chkmax(int &x, int y) {
  if (y > x) x = y;
}
int n, ans, mx, root;
int a[maxn], sum[maxn];
void Solve(int v) {
  static int b[maxn], last[maxn << 1];
  for (int i = 1; i <= n; ++i) {
    if (a[i] == root) {
      b[i] = 1;
    } else if (a[i] == v) {
      b[i] = -1;
    } else
      b[i] = 0;
  }
  int V(n);
  for (int i = 0; i <= 2 * n; ++i) last[i] = inf;
  last[V] = 0;
  int nw(0);
  for (int i = 1; i <= n; ++i) {
    nw += b[i];
    if (last[nw + V] <= i) {
      Chkmax(ans, i - last[nw + V]);
    } else
      last[nw + V] = i;
  }
}
int main() {
  scanf("%d", &n);
  mx = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    sum[a[i]]++;
    Chkmax(mx, a[i]);
  }
  root = 0;
  for (int i = 1; i <= mx; ++i) {
    if (sum[i] > sum[root]) {
      root = i;
    }
  }
  for (int i = 1; i <= mx; ++i)
    if (i ^ root) {
      Solve(i);
    }
  printf("%d\n", ans);
  return 0;
}
