Get balance noe
a[0],a[1],a[2],a[1],a[1]
1 2 3 4 5
balance noe is 3
C:
int BalanceNode(int *a, int length)
{
int *begin=a, *end=a+length;
int leftsum=0, rightsum=0;
int position=0;
while(position<length)
{
rightsum+=a[position];
position++;
}
//begin compute
position=0;
while(begin<end)
{
if(leftsum==rightsum-a[position])
return position;
leftsum+=a[position];
rightsum-=a[position];
begin++;
position++;
}
return -1;
}