Source Code
#include<iostream>

#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int mod=1e9+7;
const ll INF=1e18;
const double pi=3.14159265358979323846;

#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pb push_back
#define Go ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

using namespace std;

ll a[200010];

int main()
{
Go

int t; cin >> t;
while(t--)
{
    int n; cin >> n;
    for(int i=0;i<n;i++)cin >> a[i];
    sort(a,a+n);

    map<ll,int>mp;
    for(int i=0;i<n;i++)mp[a[i]]++;

    ll ans=0;

    for(auto it:mp)if(it.S>2){ ans=-1; break; }
    if(ans==-1){ cout << -1 << '\n'; continue; }

    ll mx=*max_element(a,a+n);
    if(mp[mx]==1)ans=n;
    else ans=mp.size();

    cout << ans << '\n';
}

return 0;
}
Copy
Proud Competitors yumna08
GNU G++17
47 ms
2.5 MB
Accepted