Source Code
#include <iostream>

using namespace std;

int t,n,a[200000],ans[10000], temp;

bool distinct(int a[], int n){
    for (int i = 0; i < n; i++){
        int temp = 0;
        for (int j = 0; j < n; j++){
            if (a[i] == a[j]){
                temp++;
            }
            if (temp>2){
                return false;
            }
        }
    }
    return true;
}

int repetition(int a[], int n){
    int ret = 0;
    for (int i = 0; i < n; i++){
        int temp = 0;
        for (int j = 0; j < n; j++){
            if (a[i] == a[j]){
                temp++;
            }
        }
        if(temp>1){
            ret++;
        }
    }
    return ret/2;
}

int main()
{
    cin >> t;
    for (int i = 0; i < t; i++){
        cin >> n;
        for (int j = 0; j < n; j++){
            cin >> a[j];
        }
        if (distinct(a,n) == false){
            ans[i] = -1;
        }
        else{
            ans[i] = n - repetition(a,n);
        }
    }
    
    for(int i = 0; i < t; i++){
        printf("%d \n", ans[i]);
    }
    return 0;
}
Copy
Proud Competitors laith.tak01
GNU G++17
1808 ms
376 KB
Wrong Answer