Hi,
In binary search, I have noticed that in some algorithms you use Left<Right and in other cases, we have left<=right. How do we determine this? is there a governing principle?
TIA
Hi,
In binary search, I have noticed that in some algorithms you use Left<Right and in other cases, we have left<=right. How do we determine this? is there a governing principle?
TIA
Hi @Harsh_Trivedi
It depends on the implementation and the logic that you are using. We can also use (l!=r). If you are using (l<r), then you have to do l=m+1
and r=m
, where m
is the mid of a sorted array, l
is the left pointer, and r
is the right pointer. While if you are using (l<=r), then as the solution shows, you would write l=m+1
and r=m-1
to make a binary search algorithm.
If you still have any confusion, please let us know.
Thanks for asking this question.
Happy learning