Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #User function template for Python
- #Method 1
- from bisect import bisect_left as bs
- class Solution:
- def binarysearch(self, a, n, k):
- idx=bs(arr,k)
- if idx<n and a[idx]==k:
- return idx
- else:return -1
- #Method 2
- from bisect import bisect_right as bs
- class Solution:
- def binarysearch(self, a, n, k):
- idx=bs(arr,k)
- if idx<n and a[idx]==k:
- return idx
- if idx>1 and a[idx-1]==k:
- return idx-1
- return -1
- #Method 3
- from bisect import bisect_left as bl,bisect_right as br
- class Solution:
- def binarysearch(self, a, n, k):
- x,y=bl(a,k),br(a,k)
- if x==y:return -1
- else:
- return x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement