Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
void MOUST(){
    ios_base::sync_with_stdio(0);
           cin.tie(0);
}
int main(){
    MOUST();

    int q;cin>>q;
    while(q--){
        ll n,k=0,p=0,d=0,c=0;cin>>n;
        vector<ll>v;
        for(int i=0;i<n;i++){
            cin>>k;
            v.push_back(k);
            p=max(p,k);
        }
        sort(v.begin(),v.end());
        v.push_back(0);
        k=0;
        for(int i=0;i<n;i++){
            if(v[i]==p)
                d++;
            if(v[i]==v[i+1]&&v[i]!=p)
                k++;
            else
                k=0;
            c=max(c,k);
        }
        c++;
        if(c>d)
            cout<<-1<<"\n";
        else
            cout<<n-c-d+2<<"\n";
    }
}
Copy
Proud Competitors call911kid
GNU G++17
18 ms
376 KB
Wrong Answer