Created
April 24, 2021 19:24
-
-
Save SamirPaulb/856bb25fc26fc26f73124efa44544741 to your computer and use it in GitHub Desktop.
To Count a repetative Element in a Sorted Array using Binary Search
This file contains 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 BS_first(ll,key): | |
left = 0 | |
right = len(ll)-1 | |
while left<=right: | |
mid = int((left+right)/2) | |
if key == ll[mid]: | |
a = mid | |
right = mid - 1 | |
elif key > ll[mid]: | |
left = mid +1 | |
else: | |
right = mid -1 | |
return a | |
def BS_last(ll,key): | |
left = 0 | |
right = len(ll)-1 | |
while left<=right: | |
mid = int((left+ right)/2) | |
if key == ll[mid]: | |
b = mid | |
left = mid + 1 | |
elif key > ll[mid]: | |
left = mid +1 | |
else: | |
right = mid -1 | |
return b | |
key = int(input("Enter the repetative number from the list ll: ")) | |
ll = [1,2,4,5,6,7,7,7,7,7,7,7,7,7,7,7,464,6565,45467,4555555] | |
print(f"The number of repetation of {key} is = {BS_last(ll,key)-BS_first(ll,key) +1}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Count of an Element in a Sorted Array