Source Code
/*
     Author: Asmaa Raafat
*/
/////////////////////////////"THINK TWICE... CODE ONCE"/////////////////////////////////.
#include <bits/stdc++.h>
#define ll long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
/*ll freqfac[22] ={1};
void getfac(int n)
{
    for(int i=1; i<=20; i++)
    {
        freqfac[i] = freqfac[i-1] * i;
    }
}*/
int main()
{
    #ifndef ONLINE_JUDGE
    //freopen("primes.in", "r", stdin);
    #endif
    fast;
    int t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        ll arr[n];
        ll cnt =1;
        for(int i=0; i<n; i++)
        {
            cin>>arr[i];
        }
        sort(arr, arr+n);
        ll c = arr[n-1];
        for(int i=0; i<n-1; i++)
        {
            if(arr[i] == c)
            {
                cnt++;
            }
        }
        if(cnt>2)
        {
            cout<<-1<<endl;
        }
        else if(cnt == 2)
        {
            cout<<n-1<<endl;
        }
        else
        {
            cout<<n<<endl;
        }
    }
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/
Copy
Proud Competitors asmaa_raafat
GNU G++17
70 ms
504 KB
Accepted