#include <bits/stdc++.h>
using namespace std;
inline int add(int _a, int _b) {
  if (_a < 0) {
    _a += 1000000007;
  }
  if (_b < 0) {
    _b += 1000000007;
  }
  if (_a + _b >= 1000000007) {
    return _a + _b - 1000000007;
  }
  return _a + _b;
}
inline int mul(int _a, int _b) {
  if (_a < 0) {
    _a += 1000000007;
  }
  if (_b < 0) {
    _b += 1000000007;
  }
  return ((long long int)((long long int)_a * (long long int)_b)) % 1000000007;
}
const int N = 2e5;
int n, ara[N + 5], ptr[N + 5], m, mp[2 * N + 5];
bool yes[2 * N + 5];
vector<int> adj[N + 5];
vector<int> vals;
pair<int, int> bra[N + 5];
int solve_individual(int no, int pivot) {
  int i, j, idx, tot, idy, l, r;
  m = 0;
  for (i = 0; i < adj[no].size(); ++i) {
    if (i == 0) {
      idy = upper_bound(adj[pivot].begin(), adj[pivot].end(), adj[no][i]) -
            adj[pivot].begin();
      --idy, tot = adj[no].size();
      for (j = max(0, idy - tot + 1); j <= idy; ++j) bra[m++] = {+n, j};
      bra[m++] = {-n, i};
      continue;
    }
    int now = adj[no][i], prv = adj[no][i - 1];
    idx = lower_bound(adj[pivot].begin(), adj[pivot].end(), prv) -
          adj[pivot].begin();
    idy = upper_bound(adj[pivot].begin(), adj[pivot].end(), now) -
          adj[pivot].begin();
    --idy;
    if (idy - idx + 1 <= 2 * adj[no].size()) {
      for (j = idx; j <= idy; ++j) bra[m++] = {+n, j};
      bra[m++] = {-n, i};
    } else {
      tot = adj[no].size();
      for (j = idx; j <= idx + tot - 1; ++j) bra[m++] = {+n, j};
      bra[m++] = {idx + tot, idy - tot};
      for (j = idy - tot + 1; j <= idy; ++j) bra[m++] = {+n, j};
      bra[m++] = {-n, i};
    }
  }
  idx = lower_bound(adj[pivot].begin(), adj[pivot].end(), adj[no].back()) -
        adj[pivot].begin();
  tot = adj[no].size();
  for (j = idx; j < adj[pivot].size() && j <= idx + tot - 1; ++j)
    bra[m++] = {+n, j};
  int cnt[2], mx = 0;
  memset(cnt, 0, sizeof(cnt));
  yes[n] = true;
  if (bra[m - 1].first == -n)
    mp[n] = n - 1;
  else if (bra[m - 1].second + 1 == adj[pivot].size())
    mp[n] = n - 1;
  else
    mp[n] = adj[pivot][bra[m - 1].second + 1] - 1;
  for (i = m - 1; i >= 0; --i) {
    if (bra[i].first == n)
      ++cnt[0];
    else if (bra[i].first == -n)
      ++cnt[1];
    else
      cnt[0] += (bra[i].second - bra[i].first + 1);
    int d = cnt[0] - cnt[1];
    if (yes[d + n]) {
      r = mp[d + n];
      if (i == 0) {
        if (bra[i].first == -n)
          l = 0;
        else if (bra[i].second == 0)
          l = 0;
        else
          l = adj[pivot][bra[i].second - 1] + 1;
      } else if (bra[i - 1].first != -n)
        l = adj[pivot][bra[i - 1].second] + 1;
      else
        l = adj[no][bra[i - 1].second] + 1;
      mx = max(mx, r - l + 1);
    } else {
      if (bra[i].first != -n && bra[i].first != n)
        r = adj[pivot][bra[i].first] - 1;
      else if (bra[i].first == -n)
        r = adj[no][bra[i].second] - 1;
      else
        r = adj[pivot][bra[i].second] - 1;
      yes[d + n] = true, mp[d + n] = r;
    }
  }
  memset(cnt, 0, sizeof(cnt));
  for (i = m - 1, yes[n] = false; i >= 0; --i) {
    if (bra[i].first == n)
      ++cnt[0];
    else if (bra[i].first == -n)
      ++cnt[1];
    else
      cnt[0] += (bra[i].second - bra[i].first + 1);
    int d = cnt[0] - cnt[1];
    yes[d + n] = false;
  }
  return mx;
}
void solve() {
  int i, j;
  for (i = 0; i < n; ++i) {
    vals.push_back(ara[i]);
  }
  sort(vals.begin(), vals.end());
  vals.erase(unique(vals.begin(), vals.end()), vals.end());
  for (i = 0; i < vals.size(); ++i) ptr[vals[i]] = i;
  for (i = 0; i < n; ++i) {
    ara[i] = ptr[ara[i]];
    adj[ara[i]].push_back(i);
  }
  int mx = 0, pivot = 0;
  for (i = 0; i < vals.size(); ++i) {
    if (adj[i].size() > mx) {
      mx = adj[i].size();
      pivot = i;
    }
  }
  int cnt = 0;
  for (i = 0; i < vals.size(); ++i) {
    if (mx == adj[i].size()) cnt++;
    if (cnt == 2) {
      printf("%d", n), puts("");
      return;
    }
  }
  for (i = mx = 0; i < vals.size(); ++i) {
    if (i == pivot) continue;
    int len = solve_individual(i, pivot);
    mx = max(mx, len);
  }
  printf("%d", mx), puts("");
}
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) scanf("%d", &ara[i]), ara[i]--;
  solve();
}
