Maximum 1s in a Row Problem
Given a matrix ARR
with dimensions N
* M
, consisting only of 0s and 1s where each row is sorted, determine the index of the row that contains the highest number of 1s. If multiple rows contain the same number of 1s, return the index of the first such row.
Input:
The first line of input includes an integer ‘T’, representing the number of test cases.
For each test case, the first line consists of two space-separated integers ‘N’ and ‘M’, indicating the number of rows and columns respectively.
The following N lines each contain M elements representing the rows of the matrix.
Output:
For each test case, output the index of the row with the maximum number of 1's. In case of a tie, return the smallest index.
Each result should be on a separate line.
Example:
Suppose we have 1 test case
Input:
3 4
0 1 1 1
0 0 1 1
1 1 1 1
Output:
2
Explanation:
The third row (zero-indexed) has the maximum number of 1s (4 ones).
Constraints:
1 <= T <= 1000
1 <= N, M <= 500
- Each row in the matrix is sorted
- Time Limit: 1 sec
Be the first one to answer
Add answer anonymously...
Top Publicis Sapient Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Publicis Sapient Software Developer
>
Publicis Sapient Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app