Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Basic
  • Last Updated : 25 Dec, 2017

Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :