Suppose that each row of an $n \times n$ array $A$ consists of 1's and 0's such that, in any row of $A$, all the 1's comes before any 0's in that row. Assume $A$ is already in memory, describe a method running in $O(n)$ for finding the the row of $A$ that contains the most 1's.
What I tried, is to start from the last column, loop down the entries in that row, and return the index of that row if that entry is 1. It's not the most naive method, but i doubt it is $O(n)$, since if the array consist of only one 1 in the first column and last row, it will loop $n^2$ times...
Any better suggestions?
def most(A):
for i in range(len(A)-1, -1, -1):
for j in range(len(A)):
if A[j,i] == 1:
return j