forked from love1024/spoj-solution-with-explanation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CANDY.cpp
60 lines (48 loc) · 1.29 KB
/
CANDY.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<bits/stdc++.h>
using namespace std;
int main() {
//variable to store number of elements
int n;
//loop till break
while(true) {
//take input
cin>>n;
//if input is -1 break
if(n == -1)
break;
//array to store number and
//sum to store sum of all numbers
int arr[n];
int sum = 0;
for(int i=0;i<n;i++) {
cin>>arr[i];
sum += arr[i];
}
//now find average of their as
//as this will be the number at
//which everyone has equal candies
int avg;
if(sum%n == 0)
avg = sum/n;
else {
//if average is not an integer
//it everyone cannot get equal
//candies so print -1
cout<<-1<<endl;
continue;
}
//count to store number of candies
//which we are going to move
//to make everyone has equal candies
int cnt = 0;
for(int i=0; i<n; i++) {
//if current bag more candies
//then move this to average and store
//the moved candies into counts
if(arr[i] > avg)
cnt += (arr[i]-avg);
}
//print the moved candies
cout<<cnt<<endl;
}
}