


Input: 'n' = 3, 'm' = 3, 'mat' = [[1, 1, 1], [0, 0, 1], [0, 0, 0]]
Output: 0
Explanation: The row with the maximum number of ones is 0 (0 - indexed).
The first input line contains two space-separated integers, ‘n’ and ‘m’ representing the number of rows and columns of the grid, respectively.
From the second line, the next ‘n’ lines represent the rows of the grid. Every row contains ‘m’ single space-separated integers.
For each test case, print the index of the row with the maximum number of ones.
Print the output of each test case in a separate line.
1 <= n <= 50
1 <= m <= 50
0 <= mat[i][j] <= 1
Where ‘n’ is the number of rows and ‘m’ is the number of columns.
Time limit: 1 sec
We will traverse each row one by one and count the number of ones of each row in a variable ‘currOnes’. We will be updating the ‘maxOnes’ by ‘currOnes’ if ‘currOnes’ is greater than ‘maxOnes’.
Finally, we return the index of the row corresponding to ‘maxOnes’.
The main observation here is that each row is already sorted in increasing order. Hence we can apply binary search in each row to find the number of ones each row has.
Here is the algorithm:
The main observation here is that instead of doing binary search in every row, we first check whether the row has more 1s than max so far. If the row has more 1s, then move until a zero is found.
Here is the algorithm: