#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9;
const long long INF = 1e15;
template <class o>
void qr(o& x) {
  char c = getchar();
  x = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  x *= f;
}
template <class o>
void qw(o x) {
  if (x / 10) qw(x / 10);
  putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar(' ');
}
template <class o>
void pr2(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar('\n');
}
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long power(long long a, long long b = mod - 2, long long p = mod) {
  long long c = 1;
  while (b) {
    if (b & 1) c = c * a % p;
    b /= 2;
    a = a * a % p;
  }
  return c;
}
long long exgcd(long long a, long long b, long long& x, long long& y) {
  if (!a) {
    x = 0;
    y = 1;
    return b;
  }
  long long d = exgcd(b % a, a, y, x);
  x -= b / a * y;
  return d;
}
long long INV(long long n, long long p = mod) {
  long long x, y;
  exgcd(n, p, x, y);
  return (x % p + p) % p;
}
template <class o>
void cmax(o& x, o y) {
  if (x < y) x = y;
}
template <class o>
void cmin(o& x, o y) {
  if (x > y) x = y;
}
template <typename t1, typename t2>
void ad(t1& x, t2 y) {
  x += y;
  if (x >= mod) x -= mod;
}
template <typename t1, typename t2>
void dl(t1& x, t2 y) {
  x -= y;
  if (x < 0) x += mod;
}
template <typename T>
struct BIT {
  T* c;
  int n;
  BIT(int _n) : n(_n) {
    c = new T[n];
    c--;
    for (int i = 1; i <= n; i++) c[i] = T(0);
  }
  void add(int x, T y) {
    for (; x <= n; x += x & -x) c[x] = c[x] + y;
  }
  T sum(int x) {
    T y = T(0);
    for (; x; x &= x - 1) y = y + c[x];
    return y;
  }
};
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  size_t operator()(pair<int, int> x) const {
    return operator()(uint64_t(x.first) << 32 | x.second);
  }
};
template <long long mod>
struct dbinom {
  int *jc, *inv;
  dbinom(int n) {
    jc = new int[n + 5];
    inv = new int[n + 5];
    jc_init(n);
  }
  void jc_init(int n) {
    jc[0] = 1;
    for (int i = 1; i <= n; i++) jc[i] = (long long)jc[i - 1] * i % mod;
    inv[n] = power(jc[n], mod - 2, mod);
    for (int i = n; i; i--) inv[i - 1] = (long long)inv[i] * i % mod;
  }
  long long C(int x, int y) {
    if (x < y || y < 0) return 0;
    return (long long)jc[x] * inv[y] % mod * inv[x - y] % mod;
  }
};
struct dsu {
  int *fa, *sz;
  dsu(int n) {
    fa = new int[n + 5];
    sz = new int[n + 5];
    for (int i = 0; i <= n; i++) fa[i] = i, sz[i] = 1;
  }
  int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); }
  bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x == y) return 0;
    if (sz[x] < sz[y]) swap(x, y);
    fa[y] = x;
    sz[x] += sz[y];
    return 1;
  }
  bool same(int x, int y) { return get(x) == get(y); }
};
int n, t, val, p, k, a[N], buf[N], c[N], cnt, now;
void add(int x, int y) {
  cnt -= c[x] == now;
  c[x] += y;
  cnt += c[x] == now;
}
void solve() {
  qr(n);
  t = sqrt(n);
  for (int i = 1; i <= n; i++) {
    qr(a[i]);
    if (++buf[a[i]] > val)
      val = buf[a[i]], p = a[i], k = 1;
    else
      k += buf[a[i]] == val;
  }
  if (val == n) return pr2(0);
  if (k > 1) return pr2(n);
  t = min(t, val);
  int ans = 0;
  for (now = 1; now <= t; now++) {
    memset(c + 1, 0, sizeof(int) * n);
    cnt = 0;
    for (int l = 1, r = 1; r <= n; r++) {
      add(a[r], 1);
      while (c[a[r]] > now) add(a[l++], -1);
      if (cnt > 1) cmax(ans, r - l + 1);
    }
  }
  for (int i = 1; i <= n; i++)
    if (buf[i] > t && i != p) {
      static int lst[N << 1];
      memset(lst, -1, sizeof lst);
      int s = n;
      lst[s] = 0;
      for (int j = 1; j <= n; j++) {
        s += a[j] == p ? 1 : -(a[j] == i);
        if (lst[s] == -1)
          lst[s] = j;
        else
          cmax(ans, j - lst[s]);
      }
    }
  pr2(ans);
}
int main() {
  int T = 1;
  while (T--) solve();
  return 0;
}
