Hi forums,
I was trying one of the SPOJ problem: https://www.spoj.com/problems/BALIFE/
Here’s my code:`#include
#include
using namespace std;
typedef long long llu;
llu avgtransfer(int arr,llu len,llu index){
llu sum1=0,sum2=0;
for(int i=0; i<index; i++) sum1 += arr[i];
for(int i=index; i<len; i++) sum2 += arr[i];
llu avg = (sum1 + sum2)/len;
if(sum1 < sum2){
return (avgindex - sum1);
}
if(sum1 > sum2){
return (avg*(len-index) - sum2);
}
return 0;
}
llu findmax(llu *arr1,llu len){
llu max = 0;
for(int i=0;i<len;i++) max = (max<arr1[i])?arr1[i]:max;
return max;
}
int main() {
while(1){
//Input
llu n=0;
cin >> n;
if(n < 1) break;
int *arr = new int[n];
llu *arr1 = new llu[n];
llu sum = 0;
for (llu i=0;i<n;i++) {cin>>arr[i]; sum += arr[i];}
if(sum%n != 0){
cout<<-1<<endl;
continue;
}
//All possible prefix sum
for(llu i=1; i<(n);i++){
arr1[i-1] = avgtransfer(arr,n,i);
//cout << arr1[i-1]<<" ";
}
//cout<<endl;
// Find maximum
cout << findmax(arr1,n)<<endl;
delete [] arr;
delete [] arr1;
}
return 0;
}
`
It’s correctly running on all the sample test cases given. But, yet i’m having a WA. Probably, i’ve counted for all edge cases, but please let me know in case i missed any of the case.