Binary Search In Action

int l = min(search_space), r = max(search_space)
int l = 0, r = nums.length - 1;
int l = 0, r = nums.length;
int mid = l + (r - l) / 2      // left/lower midint mid = l + (r - l + 1) / 2  // right/upper mid
int mid = r - (r - l) / 2 // right/upper mid
l = min(search_space), r = max(search_space)
while l < r:
mid = l + (r - l) / 2 # left / lower
if checkOk(mid):
r = mid # right moves
else:
l = mid + 1
return l
l = min(search_space), r = max(search_space)
while l < r:
mid = l + (r - l + 1) / 2 # right / upper
if checkOk(mid):
l = mid # left moves up
else:
r = mid - 1
return l

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store