Source Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()

void solve(){
	int n;
	cin >> n;
	map<int, int> fr;
	int mx = 0;
	for(int i = 0, x; i<n; ++i){
		cin >> x;
		++fr[x];
		mx = max(mx, x);
	}
	if(fr[mx] > 2){
		cout << "-1\n";
		return;
	}
	cout << n - fr[mx] + 1 << '\n';

}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--){
    	solve();
    }
}
Copy
Proud Competitors molukhyyeh
GNU G++17
46 ms
1.7 MB
Accepted