Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
Last active April 2, 2020 14:19
Show Gist options
  • Save nhudinhtuan/c0a465d890c931e9f650e2dfc886102e to your computer and use it in GitHub Desktop.
Save nhudinhtuan/c0a465d890c931e9f650e2dfc886102e to your computer and use it in GitHub Desktop.
Binary search - find the right border
def find_right_border(arr, target):
left = 0
right = len(arr) - 1
right_border = -1 # keep track of the latest larger number
while left <= right:
mid = left + (right - left) // 2
if arr[mid] <= target:
left = mid + 1
else:
right_border = mid
right = mid - 1
return right_border
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment