#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("-Ofast", "-funroll-all-loops", "-ffast-math")
using namespace std;
using LL = long long;
constexpr LL mod = 1e9 + 7;
long long ex_gcd(long long a, long long b, long long &x, long long &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  long long ret = ex_gcd(b, a % b, y, x);
  y -= a / b * x;
  return ret;
}
LL mul(LL u, LL v, LL p) {
  return (u * v - LL((long double)u * v / p) * p + p) % p;
}
LL bin(LL x, LL n, LL MOD) {
  LL ret = MOD != 1;
  for (x %= MOD; n; n >>= 1, x = x * x % MOD)
    if (n & 1) ret = ret * x % MOD;
  return ret;
}
LL norm(LL x) { return x >= mod ? (x - mod) : x; }
inline LL get_inv(LL x, LL p) { return bin(x, p - 2, p); }
inline long long get_inv(long long a) {
  long long x, y;
  ex_gcd(a, mod, x, y);
  return norm(x + mod);
}
template <class T>
inline void umin(T &x, T y) {
  x = x > y ? y : x;
}
template <class T>
inline void umax(T &x, T y) {
  x = x < y ? y : x;
}
template <class T>
inline void dec(T &x, T y) {
  x -= y;
  if (x < 0) x += mod;
}
template <class T>
inline void add(T &x, T y) {
  x += y;
  if (x >= mod) x -= mod;
}
template <typename T>
void read(T &x) {
  cin >> x;
}
template <typename T, typename... Args>
void read(T &first, Args &...args) {
  read(first);
  read(args...);
}
template <class T>
LL ctz(T a) {
  return sizeof(T) > 2 ? __builtin_ctzll(a) : __builtin_ctz(a);
}
template <class T>
inline T gcd(T _a, T _b) {
  T a = abs(_a), b = abs(_b);
  LL shift = ctz(a | b);
  for (b >>= ctz(b), a >>= ctz(a); a; a -= b, a >>= ctz(a))
    if (a < b) swap(a, b);
  return b << shift;
}
const double PI = acos(-1.0);
constexpr LL INF = 0x3f3f3f3f;
constexpr long long linf = 0x3f3f3f3f3f3f3f3f;
constexpr unsigned long long base = 2333, P_1 = 19260817, P_2 = 999998639;
constexpr LL maxn = 1e6 + 10;
LL n, ans, a[maxn], s[maxn];
vector<LL> v[maxn];
int32_t main() {
  std::ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr),
      cout << fixed << setprecision(10);
  cin >> n;
  for (LL i = (1), _i = (n + 1); i < _i; ++i)
    cin >> a[i], v[a[i]].emplace_back(i);
  LL mx = 0, mx_num = 0;
  for (LL i = 1; i <= n; i++) {
    if (int32_t(v[i].size()) > mx)
      mx_num = i, mx = int32_t(v[i].size());
    else if (int32_t(v[i].size()) == mx)
      mx_num = -1;
  }
  if (mx_num == -1) {
    cout << n << '\12';
  } else {
    s[0] = 0;
    for (LL i = 1; i <= n; i++) {
      s[i] = s[i - 1] + (a[i] == mx_num);
    }
    for (LL i = 1; i <= n; i++) {
      if (int32_t(v[i].size()) and i != mx_num) {
        vector<LL> tp(400010), p(400010);
        tp[0] = 200000;
        for (LL j = 1; j <= n; j++) {
          tp[j] = tp[j - 1] + (a[j] == mx_num) - (a[j] == i);
        }
        for (LL j = n; j >= 0; j--) {
          if (!p[tp[j]])
            p[tp[j]] = j;
          else
            umax(ans, p[tp[j]] - j);
        }
      }
    }
    cout << ans << '\12';
  }
  return 0;
}
