Python map function to find row with maximum number of 1’s

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Complexity : O(M*N)

Output:

2


My Personal Notes arrow_drop_up

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry Skilled in Big Data technologies, Data Structures, Algorithm, Software Development, App Development, Databases, Python, Java, C, and WordPress

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.