Source Code
#include <bits/stdc++.h>

using namespace std;

const int N=2e5+5;

int arr[N];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%d",&arr[i]);
		sort(arr,arr+n);
		if(n>=3&&arr[n-3]==arr[n-1])
			puts("-1");
		else if(arr[n-2]==arr[n-1])
			printf("%d\n",n-1);
		else
			printf("%d\n",n);
	}
}
Copy
Proud Competitors mahmoudbadawy
GNU G++17
24 ms
408 KB
Accepted