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

void solve(){
	int n;

	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){cin>>a[i];}
	sort(a,a+n);
	int freq=0;
	for(int i=0;i<n;i++){
		if(a[i]==a[n-1])freq++;
	}
	
	if(freq>2){
		cout<<"-1"<<endl;
			return;
	}
	if(freq==2){
		cout<<n-1<<endl;
		return;
	}
	else{
		cout<<n<<endl;
	}
//	cout<<"freq = "<<freq<<" | "<<a[n-1]<<endl;

}

int main() {
int t;

cin>>t;
while(t--){
	solve();
}
return 0;
}
Copy
Proud Competitors lianattily
GNU G++17
134 ms
328 KB
Accepted