Comments
It is better to write binary search with fixed number of iterations.
50 will be enough for any case. It will save you from TLs and WAs.