#include <bits/stdc++.h>
using namespace std;
const int B = 500;
const int maxn = 4e5 + 10;
const int base = 2e5;
int cnt[maxn], a[maxn], b[maxn];
int mi[maxn], mx[maxn];
vector<int> g[maxn];
int n;
int solve2(int c, int x) {
  memset(mi, 0x3f, sizeof mi);
  memset(mx, 0, sizeof mx);
  int res = 0;
  mi[base] = 0;
  b[0] = base;
  for (int i = 1; i <= n; ++i) {
    b[i] = (a[i] == c ? 1 : (a[i] == x ? -1 : 0));
    b[i] += b[i - 1];
    mi[b[i]] = min(i, mi[b[i]]);
    mx[b[i]] = max(i, mx[b[i]]);
    res = max(res, mx[b[i]] - mi[b[i]]);
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]), ++cnt[a[i]];
    g[a[i]].push_back(i);
  }
  int c = max_element(cnt + 1, cnt + 1 + n) - cnt;
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    if (i != c && cnt[i]) {
      ans = max(ans, solve2(c, i));
    }
  printf("%d\n", ans);
  return 0;
}
