Oddsum Even Sum Just swapping ,Clean Code C++


#1

int Solution::solve(vector &A) {
int even_sum=0;
int odd_sum=0;
for(int i=0;i<A.size();i++){
if((i+1)%2==0)
odd_sum+=A[i];
else
even_sum+=A[i];
}
int count=0;
int leftoddsum=0;
int rightoddsum=0;
int leftevensum=0;
int rightevensum=0;
int temp=0;
for(int i=0;i<A.size();i++){
if((i+1)%2==0)
{
rightoddsum=odd_sum-A[i]-leftoddsum;
rightevensum=even_sum-leftevensum;
temp=rightevensum;
rightevensum=rightoddsum;
rightoddsum=temp;
if(leftoddsum+rightoddsum==leftevensum+rightevensum)
count++;
leftoddsum+=A[i];
}
else
{
rightoddsum=odd_sum-leftoddsum;
rightevensum=even_sum-A[i]-leftevensum;
swap(rightevensum,rightoddsum);
if(leftoddsum+rightoddsum==leftevensum+rightevensum)
count++;
leftevensum+=A[i];
}
}
return count;
}