#include <bits/stdc++.h>
template <typename T>
void read(T& re) {
  re = 0;
  int re2 = 1;
  char ch = '!';
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') re2 = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') {
    re = re * 10 + ch - '0';
    ch = getchar();
  }
  re *= re2;
}
template <typename T, typename... Args>
void read(T& re, Args&... args) {
  read(re);
  read(args...);
}
template <typename T>
void write(T A) {
  if (A < 0) {
    A = -A;
    putchar('-');
  }
  if (A / 10) write(A / 10);
  putchar(A % 10 + '0');
}
int T;
const int MAXN = 2e5 + 7;
int n, n2;
int a[MAXN];
int app[MAXN], maxx1, maxx2;
std::vector<std::pair<int, int> > pos[MAXN];
int ans;
int mp[MAXN << 1];
void getans1(int col) {
  memset(mp, 0x3f, sizeof(mp));
  mp[n] = 0;
  for (int i = 1, cnt = 0; i <= n; i++) {
    if (a[i] == maxx2) cnt++;
    if (a[i] == col) cnt--;
    ans = (ans > i - mp[cnt + n] ? ans : i - mp[cnt + n]);
    mp[cnt + n] = (mp[cnt + n] > i ? i : mp[cnt + n]);
  }
  memset(mp, 0x3f, sizeof(mp));
}
int sum[MAXN];
void getans2(int col) {
  std::vector<std::pair<int, int> >::iterator I1, I2;
  for (int i = 0; i < pos[col].size(); i++) {
    for (int j = i; j < pos[col].size(); j++) {
      if (sum[pos[col][j].first] - sum[pos[col][i].first] == j - i + 1) {
        ans = (ans > pos[maxx2][sum[pos[col][j].first] + 1].first -
                           pos[maxx2][sum[pos[col][i].first]].first - 1
                   ? ans
                   : pos[maxx2][sum[pos[col][j].first] + 1].first -
                         pos[maxx2][sum[pos[col][i].first]].first - 1);
      }
      if (sum[pos[col][j].first] - sum[pos[col][i].first] == j - i - 1) {
        ans = (ans > pos[col][j].first - 1 - pos[col][i].first
                   ? ans
                   : pos[col][j].first - 1 - pos[col][i].first);
      }
    }
  }
}
int main() {
  T = 1;
  while (T--) {
    maxx1 = 0;
    read(n);
    n2 = sqrt(n);
    int can = 0;
    for (int i = 1; i <= n; i++) {
      read(a[i]);
      app[a[i]]++;
      if (app[a[i]] > maxx1) {
        maxx1 = app[a[i]];
        maxx2 = a[i];
        can = 0;
      } else {
        if (app[a[i]] == maxx1) can = 1;
        if (can) ans = (ans > i ? ans : i);
      }
    }
    memset(app, 0, sizeof(app));
    maxx1 = 0;
    for (int i = n; i >= 1; i--) {
      app[a[i]]++;
      if (app[a[i]] > maxx1) {
        maxx1 = app[a[i]];
        maxx2 = a[i];
        can = 0;
      } else {
        if (app[a[i]] == maxx1) can = 1;
        if (can) ans = (ans > n - i + 1 ? ans : n - i + 1);
      }
    }
    memset(app, 0, sizeof(app));
    maxx1 = 0;
    for (int i = 1; i <= n; i++) {
      app[a[i]]++;
      if (app[a[i]] > maxx1) {
        maxx1 = app[a[i]];
        maxx2 = a[i];
      }
    }
    pos[maxx2].push_back(std::make_pair(0, 0));
    for (int i = 1, i2; i <= n; i++) {
      i2 = pos[a[i]].size();
      pos[a[i]].push_back(std::make_pair(i, i2));
    }
    maxx1++;
    pos[maxx2].push_back(std::make_pair(n + 1, maxx1));
    for (int i = 1; i <= n; i++) {
      if (a[i] == maxx2)
        sum[i] = sum[i - 1] + 1;
      else
        sum[i] = sum[i - 1];
    }
    for (int i = 1; i <= n; i++) {
      if (!app[i]) continue;
      if (i == maxx2) continue;
      if (app[i] >= n2)
        getans1(i);
      else
        getans2(i);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
