100
92 53 86 41 77 68 80 54 17 96 89 53 64 55 2 80 28 58 77 43 70 91 71 71 78 3 25 2 15 47 60 70 95 19 47 58 57 47 91 2 23 80 86 98 98 98 57 98 98 25 98 98 57 98 98 98 98 98 25 98 98 98 98 98 57 98 98 25 98 98 57 98 98 57 98 98 25 98 98 34 98 98 34 98 98 25 98 98 24 98 98 25 89 34 76 71 91 92 22 13
 20
9 2 12 10 2 9 13 14 11 16 9 1 9 13 5 11 12 19 20 1
 100
77 95 91 95 77 54 91 77 77 42 63 48 97 85 54 63 42 70 42 97 75 77 93 64 56 88 91 85 85 64 64 88 54 70 6 3 20 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 18 48 85 75 97 20 54 64 20 85 56 77 75 42
 20
15 12 19 6 5 11 2 5 18 3 17 15 17 7 9 18 19 17 18 15
 100
92 42 58 62 24 59 62 100 92 62 54 100 62 82 91 62 100 100 62 100 100 62 92 92 92 62 92 92 62 92 62 92 88 100 100 36 62 92 16 92 62 100 62 92 35 62 100 92 92 62 93 100 100 62 76 100 62 62 92 62 92 50 100 8 7 62 100 18 92 62 92 92 100 62 32 92 92 62 37 76 100 39 62 92 62 41 62 62 19 57 100 37 92 99 100 47 62 92 62 62
 100
44 80 26 88 24 37 4 96 23 25 5 5 7 41 54 35 25 57 88 91 20 78 98 64 57 60 86 91 67 63 32 100 91 34 26 41 34 98 5 80 3 57 57 25 42 98 25 88 5 5 24 67 98 34 47 84 62 31 71 91 98 57 35 57 24 34 13 79 2 73 38 57 73 5 98 100 4 23 42 7 25 34 18 91 25 26 53 32 57 25 91 8 4 16 23 91 34 53 42 98
 100
6 20 18 88 48 85 77 70 93 90 91 54 97 75 3 56 63 95 64 42 76 19 52 45 83 96 7 60 71 58 34 2 98 66 69 43 67 46 89 13 81 1 59 8 10 26 14 86 99 4 41 32 92 5 84 65 62 50 36 74 16 73 35 27 9 38 72 23 15 17 30 33 11 53 24 82 79 57 49 21 68 22 39 47 25 61 51 31 6 37 78 94 44 100 29 87 12 55 80 40
 20
13 16 6 13 19 7 18 5 17 8 11 13 19 11 19 18 18 13 14 1
 20
2 3 5 4 3 2 1 2 2 2 2 3 2 5 2 1 1 4 5 3
 100
53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 28 28 28 28 28 28 28 28 28 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 57 57 57 57 57 57 57 57 57 57 57 57 57 96 57 94 94 94 94 94 94 94 94 94 94 94 22 22 22 22 22 22 22 22 22 22 22 22 22 22
 100
94 77 52 53 56 88 23 46 33 28 11 96 68 84 4 91 57 20 98 75 89 83 22 67 80 16 54 41 27 34 62 69 5 50 32 42 45 55 36 59 60 18 37 63 90 1 64 7 81 29 39 93 82 48 43 61 17 66 8 79 2 26 6 44 31 86 40 73 3 65 12 78 74 25 87 95 24 92 13 58 85 76 100 70 38 71 10 19 97 14 47 72 99 35 9 21 51 49 30 15

47
 14
 41
 20
 79
 82
 99
 19
 9
 96
 100

#include <bits/stdc++.h>
using namespace std;
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void yes(bool t = 1) { cout << yesno[t] << endl; }
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S>
void scan(pair<T, S> &p) {
  scan(p.first), scan(p.second);
}
template <class T>
void scan(vector<T> &);
template <class T>
void scan(vector<T> &a) {
  for (auto &i : a) scan(i);
}
template <class T>
void scan(T &a) {
  cin >> a;
}
void IN() {}
template <class Head, class... Tail>
void IN(Head &head, Tail &...tail) {
  scan(head);
  IN(tail...);
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
  return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  return (a > b ? a = b, 1 : 0);
}
vector<int> iota(int n) {
  vector<int> a(n);
  iota(begin(a), end(a), 0);
  return a;
}
template <typename T>
vector<int> iota(vector<T> &a, bool greater = false) {
  vector<int> res(a.size());
  iota(begin(res), end(res), 0);
  sort(begin(res), end(res), [&](int i, int j) {
    if (greater) return a[i] > a[j];
    return a[i] < a[j];
  });
  return res;
}
template <class T>
T POW(T x, int n) {
  T res = 1;
  for (; n; n >>= 1, x *= x)
    if (n & 1) res *= x;
  return res;
}
vector<pair<long long, long long>> factor(long long x) {
  vector<pair<long long, long long>> ans;
  for (long long i = 2; i * i <= x; i++)
    if (x % i == 0) {
      ans.push_back({i, 1});
      while ((x /= i) % i == 0) ans.back().second++;
    }
  if (x != 1) ans.push_back({x, 1});
  return ans;
}
template <class T>
vector<T> divisor(T x) {
  vector<T> ans;
  for (T i = 1; i * i <= x; i++)
    if (x % i == 0) {
      ans.push_back(i);
      if (i * i != x) ans.push_back(x / i);
    }
  return ans;
}
template <typename T>
void zip(vector<T> &x) {
  vector<T> y = x;
  sort(begin(y), end(y));
  for (int i = 0; i < x.size(); ++i) {
    x[i] = distance((y).begin(), lower_bound(begin(y), end(y), (x[i])));
  }
}
int popcount(long long x) { return __builtin_popcountll(x); }
int in() {
  int x;
  cin >> x;
  return x;
}
long long lin() {
  unsigned long long x;
  cin >> x;
  return x;
}
template <typename T>
struct edge {
  int from, to;
  T cost;
  int id;
  edge(int to, T cost) : from(-1), to(to), cost(cost) {}
  edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
  edge(int from, int to, T cost, int id)
      : from(from), to(to), cost(cost), id(id) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
using Tree = vector<vector<int>>;
using Graph = vector<vector<int>>;
template <class T>
using Wgraph = vector<vector<edge<T>>>;
Graph getG(int n, int m = -1, bool directed = false, int margin = 1) {
  Tree res(n);
  if (m == -1) m = n - 1;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a -= margin, b -= margin;
    res[a].emplace_back(b);
    if (!directed) res[b].emplace_back(a);
  }
  return move(res);
}
template <class T>
Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {
  Wgraph<T> res(n);
  if (m == -1) m = n - 1;
  while (m--) {
    int a, b;
    T c;
    cin >> a >> b >> c;
    a -= margin, b -= margin;
    res[a].emplace_back(b, c);
    if (!directed) res[b].emplace_back(a, c);
  }
  return move(res);
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = begin(v); it != end(v); ++it) {
    if (it == begin(v))
      os << *it;
    else
      os << " " << *it;
  }
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <class S, class T>
string to_string(pair<S, T> p) {
  return "(" + to_string(p.first) + "," + to_string(p.second) + ")";
}
template <class A>
string to_string(A v) {
  if (v.empty()) return "{}";
  string ret = "{";
  for (auto &x : v) ret += to_string(x) + ",";
  ret.back() = '}';
  return ret;
}
void dump() { cerr << endl; }
template <class Head, class... Tail>
void dump(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  dump(tail...);
}
template <typename T>
static constexpr T inf = numeric_limits<T>::max() / 2;
struct Setup_io {
  Setup_io() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);
  }
} setup_io;
constexpr int N = 200001;
int a[N];
struct Mo {
  int n;
  vector<pair<int, int>> lr;
  explicit Mo(int n) : n(n) {}
  void add(int l, int r) { lr.emplace_back(l, r); }
  template <typename AL, typename AR, typename EL, typename ER, typename O>
  void build(const AL &add_left, const AR &add_right, const EL &erase_left,
             const ER &erase_right, const O &out) {
    int q = (int)lr.size();
    int bs = n / min<int>(n, sqrt(q));
    vector<int> ord(q);
    iota(begin(ord), end(ord), 0);
    sort(begin(ord), end(ord), [&](int a, int b) {
      int ablock = lr[a].first / bs, bblock = lr[b].first / bs;
      if (ablock != bblock) return ablock < bblock;
      return (ablock & 1) ? lr[a].second > lr[b].second
                          : lr[a].second < lr[b].second;
    });
    int l = 0, r = 0;
    for (auto idx : ord) {
      while (l > lr[idx].first) add_left(--l);
      while (r < lr[idx].second) add_right(r++);
      while (l < lr[idx].first) erase_left(l++);
      while (r > lr[idx].second) erase_right(--r);
      out(idx);
    }
  }
  template <typename A, typename E, typename O>
  void build(const A &add, const E &erase, const O &out) {
    build(add, add, erase, erase, out);
  }
};
int freq[N], s[N + 1];
int main() {
  int n;
  IN(n);
  for (long long i = 0; i < n; ++i) cin >> a[i];
  for (long long i = 0; i < n; ++i) a[i]--;
  const int T = 451;
  vector<int> cnt(n);
  for (long long i = 0; i < n; ++i) cnt[a[i]]++;
  int ma = 0, idx;
  int ans = 0;
  for (long long i = 0; i < n; ++i)
    if (chmax(ma, cnt[i])) idx = i;
  {
    vector<int> _mp(n * 2 + 1, -1);
    auto mp = _mp.begin() + n;
    for (long long t = 0; t < n; ++t) {
      if (t == idx) continue;
      if (cnt[t] < T) continue;
      mp[0] = 0;
      int cnt = 0;
      for (long long i = 0; i < n; ++i) {
        if (a[i] == idx)
          cnt++;
        else if (a[i] == t)
          cnt--;
        if (mp[cnt] != -1)
          chmax(ans, i + 1 - mp[cnt]);
        else
          mp[cnt] = i + 1;
      }
      for (long long i = -n; i <= n; ++i) mp[i] = -1;
    }
  }
  vector<int> id;
  for (long long i = 0; i < n; ++i)
    if (a[i] == idx) id.emplace_back(i + 1);
  for (long long i = 0; i < n; ++i) freq[a[i]] = 0, s[0]++;
  id.insert(id.begin(), 0);
  for (auto e : id) {
    int cnt = 0;
    for (long long i = e; i <= n - 1; ++i) {
      if (a[i] == idx) {
        cnt++;
        if (cnt == T) break;
      } else {
        s[freq[a[i]]]--;
        s[++freq[a[i]]]++;
      }
      if (s[cnt]) chmax(ans, i - e);
    }
    cnt = 0;
    for (long long i = e; i <= n - 1; ++i) {
      if (a[i] == idx) {
        cnt++;
        if (cnt == T) break;
      } else {
        s[freq[a[i]]--]--;
        s[freq[a[i]]]++;
      }
    }
  }
  cout << ans << '\n';
}
