Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.
Examples:
Example Input: matrix = [[0, 1, 1, 1], [0, 0, 1, 1], [1, 1, 1, 1], [0, 0, 0, 0]] Output: 2
We have existing solution for this problem please refer Find the row with maximum number of 1’s. We can solve this problem in python quickly using map() function. Approach is very simple, find sum of all 1’s in each row and then print index of maximum sum in a list because row having maximum 1 will also have maximum sum.
# Function to find the row with maximum number of 1's def maxOnes(input): # map sum function on each row of # given matrix # it will return list of sum of all one's # in each row, then find index of maximum element result = list(map(sum,input)) print (result.index(max(result))) # Driver program if __name__ == "__main__": input = [[0, 1, 1, 1],[0, 0, 1, 1],[1, 1, 1, 1],[0, 0, 0, 0]] maxOnes(input) |
Complexity : O(M*N)
Output:
2
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Print all possible paths from the first row to the last row in a 2D array
- Find row number of a binary matrix having maximum number of 1s
- Find the row with maximum number of 1s
- Find row with maximum and minimum number of zeroes in given Matrix
- Find maximum element of each row in a matrix
- Find row with maximum sum in a Matrix
- Find the row whose product has maximum count of prime factors
- Find all matrix elements which are minimum in their row and maximum in their column
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Python - Row with Maximum Product
- Python - Dual Element row with Maximum difference
- Python - Row with Maximum Record Element
- Python program to Sort Matrix by Maximum Row element
- Python - Maximum in Row Range
- Maximum weight path ending at any element of last row in a matrix
- Replace every matrix element with maximum of GCD of row or column
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Maximum sum of elements from each row in the matrix
- Select row with maximum and minimum value in Pandas dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

