Please find flaw in my code

I have replied you on chat as well.I think your formula is incorrect.

You can see my code

it is quite easy to understand as well.

I have used binary search only