Python Program to Find maximum element of each row in a matrix
Last Updated :
29 Mar, 2023
Given a matrix, the task is to find the maximum element of each row.
Examples:
Input : [1, 2, 3]
[1, 4, 9]
[76, 34, 21]
Output :
3
9
76
Input : [1, 2, 3, 21]
[12, 1, 65, 9]
[1, 56, 34, 2]
Output :
21
65
56
Method 1: The idea is to run the loop for no_of_rows. Check each element inside the row and find for the maximum element. Finally, print the element.
Below is the implementation :
Python
# Python program to find maximum
# element of each row in a matrix
# importing numpy
import numpy
# Function to get max element
def maxelement(arr):
# get number of rows and columns
no_of_rows = len(arr)
no_of_column = len(arr[0])
for i in range(no_of_rows):
# Initialize max1 to 0 at beginning
# of finding max element of each row
max1 = 0
for j in range(no_of_column):
if arr[i][j] > max1:
max1 = arr[i][j]
# print maximum element of each row
print(max1)
# Driver Code
arr = [[3, 4, 1, 8],
[1, 4, 9, 11],
[76, 34, 21, 1],
[2, 1, 4, 5]]
# Calling the function
maxelement(arr)
Output :
8
11
76
5
Time Complexity: O(N^2), where N is the number of rows in the matrix.
Space Complexity: O(1), as no extra space is required for the algorithm.
Method 2: By calculating max element Each list of list of lists using the max() function
Python3
# Python program to find maximum
# element of each row in a matrix
# Driver Code
arr = [[3, 4, 1, 8],
[1, 4, 9, 11],
[76, 34, 21, 1],
[2, 1, 4, 5]]
for i in arr:
print(max(i))
Time complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary space: O(1), as we are only using a constant amount of extra space to store the maximum value of each row.
Another approach that could be used to find the maximum element of each row in a matrix is to use the built-in map() function. The map() function applies a given function to each element of a given iterable (such as a list or a matrix). In this case, we can use the map() function to apply the max() function to each row of the matrix.
Here is an example of how this approach could be implemented:
Python3
def find_max_element(matrix):
return list(map(lambda row: max(row), matrix))
matrix = [[3, 4, 1, 8],
[1, 4, 9, 11],
[76, 34, 21, 1],
[2, 1, 4, 5]]
max_elements = find_max_element(matrix)
print(max_elements) # [8, 11, 76, 5]
Time complexity: O(n * m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(n), as we are creating a new list to store the maximum elements of each row.
Method#4: Using the Recursive method.
The find_max_recursive function takes a matrix as input, along with an optional index variable i (which is initially set to 0) and a result list res (which is initially empty). The function returns a list of the maximum element in each row of the matrix.
The function first checks if it has reached the end of the matrix (i.e., if i is equal to the length of the matrix). If so, it returns the result list. Otherwise, it finds the maximum element in the current row using the built-in max function, and appends it to the result list. It then recursively calls itself with the index of the next row and the updated result list.
Python3
def find_max_recursive(matrix, i=0, res=[]):
if i == len(matrix):
return res
max_val = max(matrix[i])
res.append(max_val)
return find_max_recursive(matrix, i+1, res)
matrix = [[3, 4, 1, 8],
[1, 4, 9, 11],
[76, 34, 21, 1],
[2, 1, 4, 5]]
max_elements = find_max_recursive(matrix)
print(max_elements)
The time complexity of this function is O(n^2), where n is the size of the input matrix. This is because the function iterates over each element in the matrix once to find the maximum value in each row, resulting in n iterations. Additionally, finding the maximum value in each row requires iterating over each element in the row, resulting in another n iterations. Therefore, the total number of iterations is n^2.
The auxiliary space of this function is also O(n^2), as the result array 'res' is being appended with the maximum element from each row in the matrix. Since the matrix has n^2 elements, the result array will also have a maximum of n^2 elements, leading to the O(n^2) space complexity.
Method#5: Using the lambda function + list comprehension
In this method, we define a lambda function that takes a matrix as input and uses a list comprehension to print the maximum element of each row using the NumPy max() function.
Note: Before using numpy you first need to install it by using the following command: pip install numpy
Below is the code for the following method:
Python3
# Python program to find maximum
# element of each row in a matrix
# importing numpy
import numpy as np
# Function to get max element using lambda function
maxelement = lambda arr: [print(np.max(row), end = " ") for row in arr]
# Driver Code
arr = [[3, 4, 1, 8],
[1, 4, 9, 11],
[76, 34, 21, 1],
[2, 1, 4, 5]]
# Calling the function
maxelement(arr)
Output:
Time complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(1)
Please refer complete article on Find maximum element of each row in a matrix for more details!
Similar Reads
Find maximum element of each row in a matrix Given a matrix mat[][], the task is to find the maximum element of each row.Examples: Input: mat[][] = [[1, 2, 3] [1, 4, 9] [76, 34, 21]]Output :3976Input: mat[][] = [[1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]]Output :216556The idea is to run the loop for no_of_rows. Check each element inside the r
4 min read
Python Program to Find Largest Element in an Array To find the largest element in an array, iterate over each element and compare it with the current largest element. If an element is greater, update the largest element. At the end of the iteration, the largest element will be found. Given an array, find the largest element in it. Input : arr[] = {1
4 min read
Python - Maximum sum of elements of list in a list of lists In this problem, we need to find the maximum sum of elements in a list of lists. A list of lists is a collection of sub-lists and each sub-list contains individual elements. The problem is to find the maximum sum among all the sub-lists. Let's explore various ways to solve this problem.Using sum() w
3 min read
Python Program for Maximum size square sub-matrix with all 1s Write a Python program for a given binary matrix, the task is to find out the maximum size square sub-matrix with all 1s. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Approach: Let the given binary matrix be M[R][C]. The idea of the algorithm is to construct an
4 min read
Find the maximum and minimum element in a NumPy array An array can be considered as a container with the same types of elements. Python has its array module named array. We can simply import the module and create our array. But this module has some of its drawbacks. The main disadvantage is we can't create a multidimensional array. And the data type mu
4 min read
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: 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 t
1 min read