#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool flag = false;
  while (!isdigit(c)) {
    if (c == '-') flag = true;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
  if (flag) x = -x;
}
using namespace std;
inline void MAX(int &a, int b) {
  if (a < b) a = b;
}
const int B = 200;
int n, a[201000];
vector<int> pos[201000];
int cnt[201000];
int mx = 0, mxp = 0;
int ans;
namespace jzpA {
int bin[201000 + 201000];
inline void sol(int col) {
  memset(bin, -1, sizeof(bin));
  bin[n] = 0;
  int x = 0, u = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == mxp)
      ++x;
    else if (a[i] == col)
      ++u;
    if (~bin[x - u + n])
      MAX(ans, i - bin[x - u + n]);
    else
      bin[x - u + n] = i;
  }
}
}  // namespace jzpA
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]), pos[a[i]].push_back(i), ++cnt[a[i]];
    if (cnt[a[i]] > mx) mx = cnt[a[i]], mxp = a[i];
  }
  for (int i = 1; i <= n; ++i)
    if (pos[i].size() && i != mxp) {
      jzpA::sol(i);
    }
  printf("%d\n", ans);
  return 0;
}
