Whenever I revisit binary search problems, I keep having to work out the edge cases whenever coming up with the algorithm. So I thought I’d make a note of the pseudo code template I always end up with:

l=0, r=0 while (l <= r) { m = l+(r-l)/2 if (a[m] == target) return m if (a[m] < target) l = m+1 else r = m-1 } return -1 //not found

This is a nice video lecture about binary search:

https://www.youtube.com/watch?v=GU7DpgHINWQ