// ......
// all codes above are the same
}else
{
b = balance( A, l, r-1, p );
if( b == 1 )
return searchHeavier( A, l, p );
else if( b == -1 )
return searchHeavier( A, p+1, r-1 );
else if( b == 0 ) // modify this part
{
b = balance( A, r-1, r, r-1 );
if( b == 0 )
return -1; // all balls are the same
else if( b == -1 )
return r;
// it's impossible that b=1
}
}
}