Python | Binary search | O(logn)


#1

class Solution:

def searchRange(self, A, B):

    return (self.left_index(A,B),self.right_index(A,B))

def left_index(self,A,B):
    left = 0
    right = len (A) - 1

    while(left <= right):

        mid = left +(right - left )//2

        if (A[mid] == B):

            if (mid-1 >= 0 and A[mid - 1 ] == B):
                left -= 1
                continue

            return mid

        elif (B < A[mid]):
            right = mid - 1

        else:
            left = mid + 1

    return -1

def right_index(self,A,B):

    left = 0
    right = len (A) - 1

    while(left <= right):

        mid = left +(right - left )//2

        if (A[mid] == B):

            if (mid+1 <= len(A)-1 and A[mid + 1 ] == B):
                right += 1
                continue
            return mid

        elif (B < A[mid]):
            right = mid - 1

        else:
            left = mid + 1

    return -1