Why this is wrong?

#1

I tried to accommodate prefix and suffix in the same array but it is giving answer+1 output in some case.

int Solution::solve(int A, vector &B) {

``````for(int i = 1 ; i < A ; i++){
B[i] = B[i]+B[i-1] ;
}

int j=0 , k=A-1;
int c = 0;
int ss=B[0] , es=B[A-1]-B[A-2] , ms ;

while(j < k){
if(ss < es){
j++ ;
ss = B[j] ;
}
else if(es < ss){
k-- ;
es = B[A-1] - B[k-1];
}
else if (es == ss){
ms = B[k-1] - B[j];
if( ms == es && ms == ss)
{ c++ ; }

if((B[j+1]-B[j]) <= (B[k-1] - B[k-2] ))
{
j++ ;
ss = B[j]  ;
}
else if ((B[j+1]-B[j]) > (B[k-1] - B[k-2] ))
{
k-- ;
es = B[A-1] - B[k-1];
}
}
}
return c ;
``````

}

#2

The part where you write

fails due to presence of zero and negative elements. you can see it yourself by printing values of j and k in each step on this example 0 0 0 0 0 0 0 0 0 (extra space for clarity) .
in this case your value of k remains at 8.

#4

But I canâ€™t understand why ?

#5

I think the cases where
es == ss and B[j+1]-B[j]) == (B[k-1] - B[k-2]
is where you are skipping solutions, because you do j++, but nothing on the right (es) side.