![]() |
VOOZH | about |
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 :
3
9
76
Input: mat[][] = [[1, 2, 3, 21]
[12, 1, 65, 9]
[1, 56, 34, 2]]
Output :
21
65
56
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.
8 11 76 5
Time Complexity:O(n*m) (where, n refers to no. of rows and m refers to no. of columns)
Auxiliary Space: O(n) (where, n refers to no. of rows)
Using List Comprehension in Python
1. Iterate through each row of the matrix.
2. Find the maximum element in the current row using the max() function.
3. Append the maximum element to the output list.
4. Return the output list.
[3, 9, 76]