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++
// C++ program to find the maximum
// element of each column.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
// Function to find the maximum
// element of each column.
void largestInColumn(int mat[][MAX], int rows, int cols)
{
for (int i = 0; i < cols; i++) {
// initialize the maximum element
// with 0
int maxm = mat[0][i];
// Run the inner loop for rows
for (int j = 1; j < rows; j++) {
// check if any element is greater
// than the maximum element
// of the column and replace it
if (mat[j][i] > maxm)
maxm = mat[j][i];
}
// print the largest element of the column
cout << maxm << endl;
}
}
// Driver code
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
// C program to find the maximum
// element of each column.
#include <stdio.h>
#define MAX 100
// Function to find the maximum
// element of each column.
void largestInColumn(int mat[][MAX], int rows, int cols)
{
for (int i = 0; i < cols; i++) {
// initialize the maximum element
// with 0
int maxm = mat[0][i];
// Run the inner loop for rows
for (int j = 1; j < rows; j++) {
// check if any element is greater
// than the maximum element
// of the column and replace it
if (mat[j][i] > maxm)
maxm = mat[j][i];
}
// print the largest element of the column
printf("%d\n",maxm);
}
}
// Driver code
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;
}
// This code is contributed by kothvvsaakash
Java
// Java program to find maximum
// element of each column in a matrix
public class GFG {
// Function to find the maximum
// element of each column.
public static void largestInColumn(int cols, int[][] arr)
{
for (int i = 0; i < cols; i++) {
// Initialize max to 0 at beginning
// of finding max element of each column
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);
}
}
// Driver code
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 } };
// Calling the function
largestInColumn(4, arr);
}
}
Python3
# Python3 program to find the maximum
# element of each column
MAX = 100
# function to find the maximum
# elements of each column
def largestInColumn(mat, rows, cols):
for i in range(cols):
# initialize the maximum element with 0
maxm = mat[0][i]
# run the inner loop for news
for j in range(rows):
# check if any elements is greater
# than the maximum elements
# of the column and replace it
if mat[j][i] > maxm:
maxm = mat[j][i]
# print the largest element
# of the column
print(maxm)
# Driver code
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);
# This code is contributed
# by Mohit kumar 29 (IIIT gwalior)
C#
// C# program to find maximum
// element of each column in a matrix
using System;
class GFG
{
// Function to find the maximum
// element of each column.
public static void largestInColumn(int cols,
int[, ] arr)
{
for (int i = 0; i < cols; i++)
{
// Initialize max to 0 at beginning
// of finding max element of each column
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);
}
}
// Driver code
public static void Main()
{
int[, ] arr = new int[, ] { { 3, 4, 1, 8 },
{ 1, 4, 9, 11 },
{ 76, 34, 21, 1 },
{ 2, 1, 4, 5 } };
// Calling the function
largestInColumn(4, arr);
}
}
// This code is contributed
// by Akanksha Rai
PHP
<?php
// PHP program to find the maximum
// element of each column.
$MAX = 100;
// Function to find the maximum
// element of each column.
function largestInColumn($mat, $rows, $cols)
{
for ($i = 0; $i < $cols; $i++)
{
// initialize the maximum element
// with 0
$maxm = $mat[0][$i];
// Run the inner loop for rows
for ($j = 1; $j < $rows; $j++)
{
// check if any element is greater
// than the maximum element
// of the column and replace it
if ($mat[$j][$i] > $maxm)
$maxm = $mat[$j][$i];
}
// print the largest element
// of the column
echo $maxm, "\n";
}
}
// Driver code
$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);
// This code is contributed by Sach_Code
?>
JavaScript
<script>
// Javascript program to find maximum
// element of each column in a matrix
// Function to find the maximum
// element of each column.
function largestInColumn(cols,arr)
{
for (let i = 0; i < cols; i++)
{
// Initialize max to 0 at beginning
// of finding max element of each column
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>");
}
}
// Driver code
let arr = [[ 3, 4, 1, 8 ],
[ 1, 4, 9, 11 ],
[ 76, 34, 21, 1 ],
[ 2, 1, 4, 5 ]];
// Calling the function
largestInColumn(4, arr);
// This code is contributed by sravan kumar G
</script>
Time Complexity: O(n * m), Here n is No. of Rows and m is No. of Column.
Auxiliary Space: O(1)
Similar Reads
Minimum element of each row and each column in a matrix Given a matrix, the task is to find the minimum element of each row and each column. Examples: Input: [1, 2, 3] [1, 4, 9] [76, 34, 21] Output: Minimum element of each row is {1, 1, 21} Minimum element of each column is {1, 2, 3} Input: [1, 2, 3, 21] [12, 1, 65, 9] [11, 56, 34, 2] Output: Minimum ele
11 min read
Find sum of all Matrix elements Given a matrix mat[][], the task is to find the sum of all the elements of the matrix. Examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}}Output: 21Explanation: Here sum of all element = 1 + 2 + 3 + 4 + 5 + 6 = 21 Input: mat[][] = {{4, 5, 3, 2}, {9, 5, 6, 2}, {1, 5, 3, 5}}Output: 50Explanation: Here s
5 min read
Find all matrix elements which are minimum in their row and maximum in their column Given a matrix mat[][] of size M * N, the task is to find all matrix elements which are minimum in their respective row and maximum in their respective column. If no such element is present, print -1. Examples: Input: mat[][] = {{1, 10, 4}, {9, 3, 8}, {15, 16, 17}}Output: 15Explanation:15 is the onl
7 min read
Program to find the Sum of each Row and each Column of a Matrix Given a matrix mat of size m à n, the task is to compute the sum of each row and each column of the matrix.Examples:Input: mat = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ] Output: Sum of row 0 = 10 Sum of row 1 = 26 Sum of row 2 = 42 Sum of row 3 = 58 Sum of column 0 = 28 Sum
7 min read
Find row and column pair in given Matrix with equal row and column sum Given a matrix Mat of size N x M, the task is to find all the pairs of rows and columns where the sum of elements in the row is equal to the sum of elements in the columns. Examples: Input: M = {{1, 2, 2}, {1, 5, 6}, {3, 8, 9}}Output: {{1, 1}}Explanation: The sum of elements of rows and columns of m
8 min read