Last active
April 2, 2020 14:19
-
-
Save nhudinhtuan/c0a465d890c931e9f650e2dfc886102e to your computer and use it in GitHub Desktop.
Binary search - find the right border
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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