Monday 28 November 2016

MKEQUAL

Logic : 

            we can make N - 1 equal easily by choosing a single element for decreasing.
             so according to first statement answer will be atleast N - 1 now if see this test case
             
             N = 4   Arr = {1, 9, 1, 9}
             
answer is 4 because we can make 5 by choosing 1, 9 as pair and perform operation.
             
             that mean answer can be N also , now question is when it can be happen and why ?
             
 answer of above question is :
            
              If sum of numbers is divisible by n then all element changed to sum_element/n by 
              choosing two  index one with value greater than sum_element/n and other with
              smaller value at a time.



Below you can see code also


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
using namespace std; 
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
template<class T> void chmax(T & a, const T & b) { a = max(a, b); }
template<class T> void chmin(T & a, const T & b) { a = min(a, b); }
/////////////////////////////////////////////////////////////////////(



int main()

int T;
scanf("%d", &T);

while(T--) {
int N;
scanf("%d", &N);
long long sum = 0;
for(int i = 0; i < N; ++i) {
long long data;
scanf("%lld", &data);
sum += data;
}

printf("%d\n", N - (sum%N == 0 ? 0 : 1));
}

    return 0;
}


No comments:

Post a Comment