Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SamirPaulb/856bb25fc26fc26f73124efa44544741 to your computer and use it in GitHub Desktop.
Save SamirPaulb/856bb25fc26fc26f73124efa44544741 to your computer and use it in GitHub Desktop.
To Count a repetative Element in a Sorted Array using Binary Search
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}")
@SamirPaulb
Copy link
Author

Count of an Element in a Sorted Array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment