Making Binary Search My Bitch!
so started this first proper algorithm after studying the first topic from the coursera course that i took on dsa. solved some problems on the same topic and these are my learnings.
Binary search is ez. there is this simple template that you need to follow and you will good to go.
first when binary search? for all the problems that have a sorted array and you need to find something the first algorithm that should pop in your mind is Binary search. why? the time complexity is fast for a n sized array (linear search tc is about O(n)) is about O(n*logn).
the basic template that you need to follow is as follows:
l,r = 0, len(nums)-1
while l < r :
m = (l+r)//2
if nums[m] == target_to_find:
return m
elif nums[m] > target_to_find:
l = m-1
else:
r = m+1
return -1 # if nothing found