I hope everyone is doing well.
I know there are a lot of questions whose title is very much similar to my question but I have a doubt regarding start and end values :-
If I use stopping condition as
start<=end
then, whether start=0 ,end=n/n-1|start=-1,end=n-1/n|start=-2 to end=n-2| start=-3 to end=n-3 **the output is same in all these cases for the below codewhile(l<=r) { int mid=(l+r)/2;
if(arr[mid]==k) return mid; else if(k>arr[mid]) l=mid+1; else r=mid-1; }
From start=-4 to end=-4 the result is wrong.
I've read the other questions and learned the fact that changing the stopping condition changes the range of start and end (inclusive/ exclusive ) but,
Why is binary search working for start=0/-1/-2/-3 to end=n/n+1/n+2/n+3? I mean apart from starting mid=(n-2+2)/2<=> n/2, there may be a condition when the target element appears at arr[0].
Thanks for spending your precious time in solving my query.
I hope I've written clearly.