Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
Last active June 9, 2021 16:40
Show Gist options
  • Save nhudinhtuan/0c3f14e6b92ef0c8615e73d97baa7a8b to your computer and use it in GitHub Desktop.
Save nhudinhtuan/0c3f14e6b92ef0c8615e73d97baa7a8b to your computer and use it in GitHub Desktop.
Binary search - find the left border
def find_left_border(arr, target):
left = 0
right = len(arr) - 1
left_border = -1 # keep track of the latest smaller number
while left <= right:
mid = left + (right - left) // 2
if arr[mid] < target:
left_border = mid
left = mid + 1
else: # in case arr[mid] >= target
right = mid - 1
return left_border
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment