Reputation: 69
here is the ideone link for the source code for you to test your edits: https://ideone.com/3yDtFb here is the link for the problem:http://www.spoj.com/problems/CANDY/ it appears to give me WA on spoj despite passing given test cases with correct format,here's the code.
#include<iostream>
using namespace std;
int main()
{
long long int i,n,each,min;
cin>>n;
while(n!=-1)
{
long long int a[n],sum=0;
for(i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
if(sum%n!=0)
cout<<-1;
else
{
each=sum/n;
for(i=0;i<n;i++)
{
if(a[i]>each)
min=(a[i]-each);
}
cout<<min<<endl;
}
cin>>n;
}
return 0;
}
Upvotes: 1
Views: 518
Reputation: 11
Even I had the same query. Around half an hour later and going through EVERY iteration, it clicked : variables 'sum' and 'min' were getting carried on to the next test case. Thus, initialize them in the outer 'while' scope.
Upvotes: 1
Reputation: 311
You lost min=0
, min+=
and cout<<-1<<endl
min=0;
each=sum/n;
for(i=0;i<n;i++)
{
if(a[i]>each)
min+=(a[i]-each);
}
cout<<min<<endl;
Upvotes: 0