1538B - Friends and Candies - CodeForces Solution


greedy math *800

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    count = 0
    n = int(input())
    candies = [int(x) for x in input().split()]
    min = sum(candies) / n
    
    if min % 1 != 0:
        print(-1)
        continue

    for c in candies:
        if c > min:
            count += 1

    print(count)

C++ Code:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};


void solve() {
    ll n;
    cin >> n;
    vector<ll> a(n);

    ll s = 0;
    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
        s += a[i];
    }

    if (s % n != 0) {
        cout << -1 << "\n";
        return;
    }

    ll ans = 0;

    for (auto x: a) {
        ans += (x > s / n);
    }

    cout << ans << "\n";
}


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

    ll tst = 1;
    cin >> tst;

    while (tst--) {
        solve();
    }


    return 0;
}


Comments

Submit
0 Comments
More Questions

1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush