#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 200005, M = 102;
int n, m;
int a[N];
int p[N][M];
vector<int> v[M];
int tor(int l, int r) {
  while (r + 1 <= n) {
    ++r;
    int max1 = 0, max2 = 0;
    for (int i = 1; i <= m; ++i) {
      if (p[r][i] - p[l - 1][i] >= max1) {
        max2 = max1;
        max1 = p[r][i] - p[l - 1][i];
      } else if (p[r][i] - p[l - 1][i] >= max2)
        max2 = p[r][i] - p[l - 1][i];
    }
    if (max1 == max2) continue;
    return r - l;
  }
  return r - l + 1;
}
void solv() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    v[a[i]].push_back(i);
  }
  m = min(n, 100);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) p[i][j] = p[i - 1][j];
    p[i][a[i]]++;
  }
  int ans = 0;
  for (int r = 1; r <= n; ++r) {
    int k =
        lower_bound((v[a[r]]).begin(), (v[a[r]]).end(), r) - v[a[r]].begin();
    int l = r - ans;
    while (l >= 1) {
      int max1 = 0, max2 = 0;
      for (int i = 1; i <= m; ++i) {
        if (p[r][i] - p[l - 1][i] >= max1) {
          max2 = max1;
          max1 = p[r][i] - p[l - 1][i];
        } else if (p[r][i] - p[l - 1][i] >= max2)
          max2 = p[r][i] - p[l - 1][i];
      }
      if (max1 == max2) {
        ans = tor(l, r);
        l = r - ans;
      } else {
        if (k - max1 + 1 < 0) break;
        int u = v[a[r]][k - max1 + 1];
        l = min(l - (max1 - max2), u);
      }
    }
  }
  printf("%d\n", ans);
}
int main() {
  solv();
  return 0;
}
