#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  x = 0;
  bool f = false;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') {
      f = true;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}
template <typename T>
inline void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 ^ 48);
  return;
}
const int N = 2e5 + 5, T = 505, INF = 1e9 + 7;
int n, Maxn, cnt, Ans, S, val;
int a[N], Num[N], sum[N], t[N * 2], cnt1[N], cnt2[N];
vector<int> v;
inline void Modify(int x, int v) {
  cnt2[cnt1[x]]--, cnt1[x] += v, cnt2[cnt1[x]]++;
}
int main() {
  read(n), S = sqrt(n);
  for (int i = 1; i <= n; i++) read(a[i]), Num[a[i]]++;
  for (int i = 1; i <= n; i++) {
    if (Num[i] == Maxn) cnt++;
    if (Num[i] > Maxn) cnt = 1, Maxn = Num[i];
  }
  if (cnt > 1) {
    write(n);
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    if (Num[i] == Maxn)
      val = i;
    else if (Num[i] > S)
      v.push_back(i);
  }
  for (int i = 0; i < v.size(); i++) {
    int k = v[i];
    for (int j = 1; j <= n; j++)
      sum[j] = sum[j - 1] + (a[j] == k ? -1 : (a[j] == val ? 1 : 0));
    for (int j = -n; j <= n; j++) t[n + j] = INF;
    for (int j = 1; j <= n; j++)
      Ans = max(Ans, j - t[n + sum[j]] + 1),
      t[n + sum[j - 1]] = min(t[n + sum[j - 1]], j);
  }
  for (int i = 1; i <= S; i++) {
    for (int j = 1; j <= n; j++) cnt1[j] = cnt2[j] = 0;
    int l = 1, r = 0;
    while (r < n) {
      r++, Modify(a[r], 1);
      while (cnt1[a[r]] > i) Modify(a[l], -1), l++;
      if (cnt2[i] >= 2) Ans = max(Ans, r - l + 1);
    }
  }
  write(Ans);
  return 0;
}
