Can someone please help me in binary search. I am confused whether to use
--> l=0 , h = arr.size()-1 and m = (l+h)/2 and while(l<h)
--> l=0 , h = arr.size() and m = (l+h+1)/2 and while(l<h)
--> l=0 , h = arr.size()-1 and m = l+(h-l)/2 and while(l<h)
--> l=0 , h = arr.size()-1 and m = (l+h)/2 and while(l<=h)
It seems that in some cases some of the above give TLE. Can some experienced community member help me which I should use .
I think many of the beginners face this issue while implementing binary search . A little help would be appreciated
So basically there are confusion regarding
while(l<h) or while(l<=h) or while(h-l>1)
m=(l+h+1)/2 or m=l+(h-l)/2 or m=(l+h)/2
h=arr.size() or h=arr.size()-1