Find maximum element of each column in a matrix
Last Updated :
15 Sep, 2022
Given a matrix, the task is to find the maximum element of each column.
Examples:
Input: [1, 2, 3]
[1, 4, 9]
[76, 34, 21]
Output:
76
34
21
Input: [1, 2, 3, 21]
[12, 1, 65, 9]
1, 56, 34, 2]
Output:
12
56
65
21
Approach: The idea is to run the loop for no_of_cols. Check each element inside the column and find the maximum element. Finally, print the element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
void largestInColumn( int mat[][MAX], int rows, int cols)
{
for ( int i = 0; i < cols; i++) {
int maxm = mat[0][i];
for ( int j = 1; j < rows; j++) {
if (mat[j][i] > maxm)
maxm = mat[j][i];
}
cout << maxm << endl;
}
}
int main()
{
int n = 4, m = 4;
int mat[][MAX] = { { 3, 4, 1, 8 },
{ 1, 4, 9, 11 },
{ 76, 34, 21, 1 },
{ 2, 1, 4, 5 } };
largestInColumn(mat, n, m);
return 0;
}
|
C
#include <stdio.h>
#define MAX 100
void largestInColumn( int mat[][MAX], int rows, int cols)
{
for ( int i = 0; i < cols; i++) {
int maxm = mat[0][i];
for ( int j = 1; j < rows; j++) {
if (mat[j][i] > maxm)
maxm = mat[j][i];
}
printf ( "%d\n" ,maxm);
}
}
int main()
{
int n = 4, m = 4;
int mat[][MAX] = { { 3, 4, 1, 8 },
{ 1, 4, 9, 11 },
{ 76, 34, 21, 1 },
{ 2, 1, 4, 5 } };
largestInColumn(mat, n, m);
return 0;
}
|
Java
public class GFG {
public static void largestInColumn( int cols, int [][] arr)
{
for ( int i = 0 ; i < cols; i++) {
int maxm = arr[ 0 ][i];
for ( int j = 1 ; j < arr[i].length; j++)
if (arr[j][i] > maxm)
maxm = arr[j][i];
System.out.println(maxm);
}
}
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 } };
largestInColumn( 4 , arr);
}
}
|
Python3
MAX = 100
def largestInColumn(mat, rows, cols):
for i in range (cols):
maxm = mat[ 0 ][i]
for j in range (rows):
if mat[j][i] > maxm:
maxm = mat[j][i]
print (maxm)
n, m = 4 , 4
mat = [[ 3 , 4 , 1 , 8 ],
[ 1 , 4 , 9 , 11 ],
[ 76 , 34 , 21 , 1 ],
[ 2 , 1 , 4 , 5 ]]
largestInColumn(mat, n, m);
|
C#
using System;
class GFG
{
public static void largestInColumn( int cols,
int [, ] arr)
{
for ( int i = 0; i < cols; i++)
{
int maxm = arr[0, i];
for ( int j = 1; j < arr.GetLength(0); j++)
if (arr[j, i] > maxm)
maxm = arr[j, i];
Console.WriteLine(maxm);
}
}
public static void Main()
{
int [, ] arr = new int [, ] { { 3, 4, 1, 8 },
{ 1, 4, 9, 11 },
{ 76, 34, 21, 1 },
{ 2, 1, 4, 5 } };
largestInColumn(4, arr);
}
}
|
PHP
<?php
$MAX = 100;
function largestInColumn( $mat , $rows , $cols )
{
for ( $i = 0; $i < $cols ; $i ++)
{
$maxm = $mat [0][ $i ];
for ( $j = 1; $j < $rows ; $j ++)
{
if ( $mat [ $j ][ $i ] > $maxm )
$maxm = $mat [ $j ][ $i ];
}
echo $maxm , "\n" ;
}
}
$n = 4;
$m = 4;
$mat = array ( array ( 3, 4, 1, 8 ),
array ( 1, 4, 9, 11 ),
array ( 76, 34, 21, 1 ),
array ( 2, 1, 4, 5 ));
largestInColumn( $mat , $n , $m );
?>
|
Javascript
<script>
function largestInColumn(cols,arr)
{
for (let i = 0; i < cols; i++)
{
let maxm = arr[0][i];
for (let j = 1; j < arr[i].length; j++)
if (arr[j][i] > maxm)
maxm = arr[j][i];
document.write(maxm+ "<br>" );
}
}
let arr = [[ 3, 4, 1, 8 ],
[ 1, 4, 9, 11 ],
[ 76, 34, 21, 1 ],
[ 2, 1, 4, 5 ]];
largestInColumn(4, arr);
</script>
|
Time Complexity: O(n * m), Here n is No. of Rows and m is No. of Column.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...