You are given a matrix ‘ARR’ with dimensions ‘N’ * ‘M’ and containing only 0s and 1s where each row is sorted.
Your task is to find the index of the row with a maximum number of 1s in it. Rows and columns are 0-indexed based.
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, denoting the number of rows and the number of columns respectively.
Then each of the next N lines contains M elements.
Output format:
For each test case, return the index of the row, which has the maximum number of 1’s in it. If more than one row has the same number of 1’s, then return the row with the lowest index(consider 0-based indexing).
Output for each test case is printed on a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 1000
1 <= N, M <= 500
Time Limit: 1 sec
2
1 1
1
3 3
0 0 1
1 1 1
0 1 1
0
1
For the first test case,
Since the matrix has one row and one column. The only row, i.e., the 0th row, contains a 1. So the output is 0.
For the second test case,
Since the matrix has three rows and three columns. The number of ones in the 0th row is 1. The number of ones in the 1st row is 3, and the number of ones in the 2nd row is 2. So the output is 1.
2
3 2
0 1
0 1
0 0
4 4
0 0 1 1
0 0 0 0
1 1 1 1
0 0 1 1
0
2
For the first test case,
Since the matrix has 3 rows and 2 columns. The number of ones in the 0th row is 1. The number of ones in the 1st row is 1, and the number of ones in the 2nd row is 0. So the output is 0.
For the second test case,
Since the matrix has 4 rows and 4 columns. The number of ones in the 0th row is 2. The number of ones in the 1st row is 0. The number of ones in the 2nd row is 4 and the number of ones in the 3rd row is 2. So the output is 2.
Traverse through each row and count the number of 1’s in it and find the maximum among them.
Approach:
Algorithm:
O(N*M), where ‘N’ is the number of rows, and the ‘M’ is the number of columns.
Since we are traversing over the matrix, which takes O(N*M) time. So overall, time complexity is O(N*M).
O(1).
Since constant space is being used. So overall, space complexity is O(1).