0w1

HR Equal ( Greedy )

https://www.hackerrank.com/challenges/equal
An important observation to make, is to realise that "adding value x to all elements except element y" is equivalent to "decreasing value x from element y". Once this is made clear, it will be obvious that the answer will be the sum of each individual cost, to become some particular value. This will somehow be like the classical coin problem, and the conclusion tells us if the possible changes are not multiple of each, the answer might vary by some value under the largest change( I will come back and scrutinise that someday later ). So we will examine such value for range [ minv - 4, minv ].

#include <bits/stdc++.h>
using namespace std;
const int MAXT = 100 + 2;
const int MAXN = 1e4 + 4;

int T;
int N;
int choc[ MAXN ];

void solve(){
    int ans = 1e9;
    int minv = *min_element( choc, choc + N );
    for( int gol = minv; gol >= 0 && gol >= minv - 4; --gol ){
        int cost = 0;
        for( int i = 0; i < N; ++i ){
            int diff = choc[ i ] - gol;
            cost += diff / 5; diff %= 5;
            cost += diff / 2; diff %= 2;
            cost += diff;
        }
        ans = min( ans, cost );
    }
    printf( "%d\n", ans );
}

int main(){
    scanf( "%d", &T );
    while( T-- ){
        scanf( "%d", &N );
        for( int i = 0; i < N; ++i )
            scanf( "%d", choc + i );
        solve();
    }
    return 0;
}