#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
const ll mod = 1e9+7;
const db eps = 1e-9 ;
const ll maxn = 2e5+1;
const ll inf = LLONG_MAX;
#define mp make_pair
#define pb push_back
#define endl "\n"
#define deb(x) cout << #x << " " << x << endl
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
ll t;
cin >> t;
while(t--)
{
map<ll,ll> cnt;
ll n;
cin >> n;
vector<ll> v(n+1);
for(ll i=1 ; i<=n ; ++i)
{
cin >> v[i];
cnt[v[i]]++;
}
ll ans = 0;
for(auto x : cnt)
{
for(auto y : cnt)
{
if( x.first == y.first )
{
ans = max(ans, y.second);
}
else
{
ll begin = 1, end = n , side_cnt=0 , center_cnt = y.second;
ans = max(ans , center_cnt);
while(begin < end)
{
while(begin < end && v[begin]!=x.first)
{
if(v[begin] == y.first) center_cnt--;
begin++;
}
while(begin < end && v[end]!=x.first)
{
if(v[end] == y.first) center_cnt--;
end--;
}
if(begin >= end) break;
side_cnt++;
ans = max(ans , center_cnt + 2*side_cnt);
begin++,end--;
}
}
}
}
cout << ans << endl;
}
return 0;
}
This gives TLE. How do I further reduce the time complexity?