I was recently solving this question using binary search(https://codeforces.me/problemset/problem/1201/C). I came to know about my mistake as my formula for calculating mid was low+(high-low)/2 but instead when I use low+(high-low+1)/2 I get AC.
My questions is how do all of you know which formula to use for calculating mid. I tried to google it and could not find any link which answers my question.