C++ O(n) time and O(1) space solution


#1

int Solution::solve(int A, vector &B) {
int total=0;
for(int i=0;i<A;i++){
total+=B[i];
}
if(total%3!=0){
return 0;
}
int sum=total/3,curr=0,i,j;
for(i=0;i<A;i++){
curr+=B[i];
if(curr==sum){
i++;
break;
}
}
curr=0;
for(j=A-1;j>=0;j–){
curr+=B[j];
if(curr==sum){
j–;
break;
}
}
int left=0,right=0;curr=0;
while(i<=j){
curr+=B[i];
if(curr==0){
left++;
}
if(curr==sum){
right++;
}
i++;
}
if(left==right){
return (left*(left+1))/2;
}
return left+right;
}


#2
if(left==right){
return (left*(left+1))/2;
}

what is the logic for this condition?