Given a 2D array containing only 0/1’s and each row is in sorted order. Find the row which contains maximum number of 1s.

def find_first_one(arr,left,right):
    while right>=left:
        mid = left+(right-left)/2
        if arr[mid]==1:
            if mid==0 or arr[mid-1]==0:
                return mid
            else:
                right=mid-1
        else:
            left = mid+1
    return None
def find_max_row(arr):
    max_row_id= 0
    max_one_index = len(arr[0])-1

    for row_id in range(len(arr)):
        if arr[row_id][max_one_index]==1:
            current_index = find_first_one(arr[row_id],0,max_one_index-1)
            if current_index and current_index<max_one_index:
                max_one_index = current_index
                max_row_id = row_id
    return max_row_id

results matching ""

    No results matching ""