Shows time limit exceeded, can't understand how and why


#1

Shows time limit exceeded, can’t understand how and why
class Solution:
# @param A : tuple of integers
# @param B : integer
# @return an integer
def findCount(self, A, B):
l = len(A)
i,j=0,l-1

    while((A[i]!=B) and (A[j]!=B) and (i!=j)):
        mid = int((i+j)/2)
        
        if A[mid]>=B:
            j = mid
        else:
            i = mid

    index = -1
    if A[i]==B:
        index=i
    elif A[j]==B:
        index=j
    
    if index==-1:
        return 0
    
    i,j=index,index
    
    while (i>=0 and A[i]==B):
        i=i-1
    i=i+1
    
    while (j<l and A[j]==B):
        j=j+1
    j-=1
    
    return j-i+1