REPLESX - understand WA


I want to understand why I am guetting wrong answer on REPLESX

Here is the code :

I have been able to have AC , just by replacing binary Search logic with while loop :

But I cannot figure out what was wrong with the binary Search method.

Thanks for your advices