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
Approach : Approach is very simple. 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.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
const int N = 4;
void printArray( int result[], int no_of_rows) {
for ( int i = 0; i < no_of_rows; i++) {
cout<< result[i]<< "\n" ;
}
}
void maxelement( int no_of_rows, int arr[][N]) {
int result[no_of_rows];
for ( int i = 0; i < no_of_rows; i++) {
int max = *max_element(arr[i], arr[i]+N);
result[i] = max;
}
printArray(result,no_of_rows);
}
int main() {
int arr[][N] = { {3, 4, 1, 8},
{1, 4, 9, 11},
{76, 34, 21, 1},
{2, 1, 4, 5} };
maxelement(4, arr);
return 0;
}
|
Java
public class GFG{
public static void maxelement( int no_of_rows, int [][] arr) {
int i = 0 ;
int max = 0 ;
int [] result = new int [no_of_rows];
while (i < no_of_rows) {
for ( int j = 0 ; j < arr[i].length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
}
}
result[i] = max;
max = 0 ;
i++;
}
printArray(result);
}
private static void printArray( int [] result) {
for ( int i = 0 ; i<result.length;i++) {
System.out.println(result[i]);
}
}
public static void main(String[] args) {
int [][] arr = new int [][] { { 3 , 4 , 1 , 8 },
{ 1 , 4 , 9 , 11 },
{ 76 , 34 , 21 , 1 },
{ 2 , 1 , 4 , 5 } };
maxelement( 4 , arr);
}
}
|
Python
import numpy
def maxelement(arr):
no_of_rows = len (arr)
no_of_column = len (arr[ 0 ])
for i in range (no_of_rows):
max1 = 0
for j in range (no_of_column):
if arr[i][j] > max1 :
max1 = arr[i][j]
print (max1)
arr = [[ 3 , 4 , 1 , 8 ],
[ 1 , 4 , 9 , 11 ],
[ 76 , 34 , 21 , 1 ],
[ 2 , 1 , 4 , 5 ]]
maxelement(arr)
|
C#
using System;
class GFG
{
public static void maxelement( int no_of_rows,
int [][] arr)
{
int i = 0;
int max = 0;
int [] result = new int [no_of_rows];
while (i < no_of_rows)
{
for ( int j = 0;
j < arr[i].Length; j++)
{
if (arr[i][j] > max)
{
max = arr[i][j];
}
}
result[i] = max;
max = 0;
i++;
}
printArray(result);
}
private static void printArray( int [] result)
{
for ( int i = 0; i < result.Length;i++)
{
Console.WriteLine(result[i]);
}
}
public static void Main( string [] args)
{
int [][] arr = new int [][]
{
new int [] {3, 4, 1, 8},
new int [] {1, 4, 9, 11},
new int [] {76, 34, 21, 1},
new int [] {2, 1, 4, 5}
};
maxelement(4, arr);
}
}
|
PHP
<?php
$N = 4;
function printArray( $result , $no_of_rows )
{
for ( $i = 0; $i < $no_of_rows ; $i ++)
{
echo $result [ $i ]. "\n" ;
}
}
function maxelement( $no_of_rows , $arr )
{
global $N ;
$i = 0;
$max = 0;
$result = array_fill (0, $no_of_rows ,0);
while ( $i < $no_of_rows )
{
for ( $j = 0; $j < $N ; $j ++)
{
if ( $arr [ $i ][ $j ] > $max )
{
$max = $arr [ $i ][ $j ];
}
}
$result [ $i ] = $max ;
$max = 0;
$i ++;
}
printArray( $result , $no_of_rows );
}
$arr = array ( array (3, 4, 1, 8),
array (1, 4, 9, 11),
array (76, 34, 21, 1),
array (2, 1, 4, 5));
maxelement(4, $arr );
?>
|
Javascript
<script>
function maxelement(no_of_rows, arr)
{
var i = 0;
var max = 0;
var result = Array.from({length: no_of_rows}, (_, i) => 0);
while (i < no_of_rows)
{
for ( var j = 0; j < arr[i].length; j++)
{
if (arr[i][j] > max)
{
max = arr[i][j];
}
}
result[i] = max;
max = 0;
i++;
}
printArray(result);
}
function printArray(result)
{
for ( var i = 0; i < result.length; i++)
{
document.write(result[i]+ "<br>" );
}
}
var arr = [[3, 4, 1, 8],
[ 1, 4, 9, 11],
[ 76, 34, 21, 1],
[ 2, 1, 4, 5] ];
maxelement(4, arr);
</script>
|
Complexity Analysis:
- 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)
METHOD: Using List Comprehension.
APPROACH:
It is a concise way of creating a list in Python. We iterate through each row in the matrix using list comprehension and find the maximum element in the current row using the max() function. We then append the maximum element to a new list called “output”. Finally, we print the “output” list which contains the maximum element of each row in the matrix.
ALGORITHM:
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.
Python3
matrix = [[ 1 , 2 , 3 ], [ 1 , 4 , 9 ], [ 76 , 34 , 21 ]]
output = [ max (row) for row in matrix]
print (output)
|
Time complexity: O(nm) where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(n) where n is the number of rows in the matrix
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Apr, 2023
Like Article
Save Article