Source Code
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<assert.h>
#include<memory.h>
#include<cmath>

using namespace std;
using ll = long long;

const int N = 1e5 + 5;


void solve(int t) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto& x : a)cin >> x;
  int mx = *max_element(a.begin(), a.end());
  int cnt = count(a.begin(), a.end(), mx);
  if (cnt > 2)return void(cout << -1);
  cout << n - (cnt > 1 ? 1 : 0);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int t = 1;
     cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    solve(tt);
    cout << '\n';
  }
}


Copy
Proud Competitors iyaad
GNU G++17
20 ms
412 KB
Accepted