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

using namespace std;

#define all(v) (v.begin()), (v.end())
#define sor(x) sort(all(x))
#define pb(x) push_back(x)
#define fl(v, d) (memset(v, d, sizeof(v)))
#define len(s) ((int)s.size())
typedef long long ll;
typedef vector<int> vii;

#define MOD ll(1e9 + 7)
#define N (const ll)1e5 + 1

inline void solve()
{
    int n;
    cin >> n;
    map<int, int> f;
    int mx = 0;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        mx = max(x, mx);
        f[x]++;
    }
    if (f[mx] > 2)
        cout << -1 << '\n';
    else if (f[mx] == 1)
        cout << n << '\n';
    else
        cout << n - 1 << '\n';
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
Copy
Proud Competitors Yazn_yousef
GNU G++17
48 ms
1.8 MB
Accepted