#include <bits/stdc++.h>
using namespace std;
template <class t>
inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c)) f |= c == '-', c = getchar();
  while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f) x = -x;
  return x;
}
template <class t, class... A>
inline void read(t &x, A &...a) {
  read(x);
  read(a...);
}
template <class t>
inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9) write(x / 10);
    putchar('0' + x % 10);
  }
}
const int N = 2e5 + 5;
mt19937 rnd(time(0));
int a[N], pos[N], n, cnt[N], ans, first, C;
vector<pair<int, int> > val;
void calc(int first, int second) {
  for (int i = 0; i <= cnt[first] + cnt[second]; i++) pos[i] = -1;
  pos[cnt[first]] = 0;
  for (int i = 1, s = 0; i <= n; i++) {
    if (a[i] == first) s--;
    if (a[i] == second) s++;
    if (~pos[cnt[first] + s])
      ans = max(ans, i - pos[cnt[first] + s]);
    else
      pos[cnt[first] + s] = i;
  }
}
signed main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    C = max(C, read(a[i]));
    if (++cnt[a[i]] > cnt[first]) first = a[i];
  }
  for (int i = 1; i <= C; i++)
    if (cnt[i] && i != first) val.emplace_back(cnt[i], i);
  if (val.empty()) return write(0), 0;
  sort(val.begin(), val.end(), greater<pair<int, int> >());
  for (int second = 1; second <= 200 && second <= val.size(); second++)
    calc(first, val[second - 1].second);
  for (int second = 1; second <= 200; second++)
    calc(first, val[rnd() % val.size()].second);
  write(ans);
}
