#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long INF = 1e14;   const long long TE3 = 1005;   const long long TE5 = 300005;   const string YN[2] = { NO ,  YES };   using namespace std;   long long f(long long n, vector<long long>& q1, vector<long long>& q2) {     unordered_map<long long, long long> e;     long long c = 0;     e[0] = -1;     long long i = 0, j = 0;     long long ans = 0;     while (i < q1.size() || j < q2.size()) {       long long g;       long long q1t = (i >= q1.size()) ? INF : q1[i];       long long q2t = (j >= q2.size()) ? INF : q2[j];       if (q1t < q2t) {         g = q1t;         ++c;         ++i;       } else {         g = q2t;         --c;         ++j;       }       if (e.find(c) == e.end()) {         e[c] = g;       } else {         q2t = (j >= q2.size()) ? INF : q2[j];         q1t = (i >= q1.size()) ? INF : q1[i];         long long tmp = min(q1t, q2t);         long long cur = (tmp == INF ? n : tmp) - e[c] - 1;         ans = max(ans, cur);       }     }     return ans;   }   int main() {     ios::sync_with_stdio(false);     long long n;     cin >> n;     long long x;     unordered_map<long long, vector<long long> > mpq;     for (long long i = (0); i < (n); ++i) {       cin >> x;       mpq[x].push_back(i);     }     if (mpq.size() == 1) {       cout << 0 << endl;       return 0;     }     unordered_map<long long, long long> rev;     long long ma = -INF, mo = 0;     for (auto it : mpq) {       rev[it.second.size()]++;       if (it.second.size() > mo) {         ma = it.first;         mo = it.second.size();       }     }     if (rev[mo] >= 2) {       cout << n << endl;       return 0;     }     long long ans = 0;     for (auto it : mpq) {       if (it.first == ma) continue;       ans = max(ans, f(n, mpq[ma], it.second));     }     cout << ans << endl;     return 0;   }   