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 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
# 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))
|
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(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:
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]
|
[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.
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)
|
[8, 11, 76, 5]
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:
# 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!