Source Code
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 9;
const int mod = 1e9 + 7;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int arr[n];
        for(int i = 0; i < n; i++) cin >> arr[i];
        sort(arr, arr + n);
        int cnt = 0;
        for(int i = n - 1; i >= 0; i--){
            if(arr[i] == arr[n - 1]) cnt++;
        }
        if(cnt > 2) cout << -1 << "\n";
        else if(cnt == 2) cout << n - 1 << "\n";
        else cout << n << "\n";
    }
    return 0;
}
Copy
Proud Competitors Mohamedmaher
GNU G++17
17 ms
1.1 MB
Accepted