Source Code
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

void solve(){
	int n;
	set<int> uniques;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++)cin>>a[i];
	
	int cnt=1, max=1;
	for(int i=0;i<n;i++){
		//cout<<a[i]<<"||"<<uniques.count(a[i])<<endl;
		if(uniques.count(a[i])==0) {uniques.insert(a[i]);
		}
		else{
			cnt++;
			uniques.clear();
			uniques.insert(a[i]);
		}
		if(max<uniques.size())max=uniques.size();
		if(cnt>2){
			cout<<"-1"<<endl;
			return;
		}
	}
	cout<<max<<endl;
}

int main() {
int t;

cin>>t;
while(t--){
	solve();
}
return 0;
}
Copy
Proud Competitors lianattily
GNU G++17
68 ms
360 KB
Wrong Answer