Source Code
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <math.h>
#include <iomanip>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <array>
#include <set>
using namespace std;


int main()
{
    int _;
    cin>>_;
    while (_--)
    {
        long long n,b,mx=0;
        cin>>n;
        bool flaj= true, fbreak=true;
        long long ary[n];
        set<long long> st1;
        map<long long, long long> mp;
        for(int i=0; i<n; i++)
        {
            cin>>ary[i];
            if (mx<ary[i])
                mx=ary[i];
            mp[ary[i]]++;
            if(mp[ary[i]]>2)
            {
                fbreak= false;
            }
            else if(mp[ary[i]]==2)
                flaj=false;
            st1.insert(ary[i]);
        }
        if(!fbreak)
            cout<<-1<<endl;
        else
        {
            if(!flaj)
            {
                if(mp[mx]==2)
                    cout<<st1.size()<<endl;
                else
                    cout<<-1<<endl;
            }
            else
                cout<<st1.size()<<endl;
        }
    }
}
Copy
Proud Competitors Abbro
GNU G++17
171 ms
2.3 MB
Wrong Answer