I have an intuition that this algorithm should have o(n) time complexity but I cannot prove it rigorously.
The question is as follows: Suppose you have an n×n 2-dimensional array A such that each row of the matrix consists of some number of 0s followed by some number of 1s. Describe a method for finding the row with the maximum number of 1s in it. What is the running time of your method? Is it possible to do it in o(n) time? If yes, prove it.
My approach:
The pseudocode is as follows: considering a 0-indexed array
col=n-1
while(a[0][col]!=0 and col>=0) col--;
for row in 1...n:
while(a[row][col]!=0 and col>=0) col--;
if(col==0) break;
Now I can say that from the perspective of moving along column, we are moving only O(n) steps, so complexity is O(n). But I cannot prove it properly. Please help.