Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Matrix
  • Practice Matrix
  • MCQs on Matrix
  • Tutorial on Matrix
  • Matrix Traversal
  • Sorting in Matrix
  • Matrix Rotation
  • Transpose of Matrix
  • Inverse of Matrix
  • Determinant of Matrix
  • Matrix Application
  • Adjoint & Inverse Matrix
  • Sparse Matrix
  • Matrix Exponentiation
Open In App
Next Article:
Find maximum element of each row in a matrix
Next article icon

Find maximum element of each column in a matrix

Last Updated : 15 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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>
 
 
Output
76 34 21 11

Time Complexity: O(n * m), Here n is No. of Rows and m is No. of Column.
Auxiliary Space: O(1)



Next Article
Find maximum element of each row in a matrix

G

gfg_sal_gfg
Improve
Article Tags :
  • DSA
  • Matrix
  • School Programming
  • Technical Scripter
  • Technical Scripter 2018
Practice Tags :
  • Matrix

Similar Reads

  • Find maximum element of each row in a matrix
    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 :3976Input: mat[][] = [[1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]]Output :216556 The idea is to run the loop for no_of_rows. Check each element inside the
    4 min read
  • 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 column with maximum sum in a Matrix
    Given a N*N matrix. The task is to find the index of column with maximum sum. That is the column whose sum of elements are maximum. Examples: Input : mat[][] = { { 1, 2, 3, 4, 5 }, { 5, 3, 1, 4, 2 }, { 5, 6, 7, 8, 9 }, { 0, 6, 3, 4, 12 }, { 9, 7, 12, 4, 3 }, }; Output : Column 5 has max sum 31 Input
    7 min read
  • Find Column with Maximum Zeros in Matrix
    Given a matrix(2D array) M of size N*N consisting of 0s and 1s only. The task is to find the column with the maximum number of 0s. If more than one column exists, print the one which comes first. If the maximum number of 0s is 0 then return -1. Examples: Input: N = 3, M[][] = {{0, 0, 0}, {1, 0, 1},
    5 min read
  • Find unique elements in a matrix
    Given a matrix mat[][] having n rows and m columns. The task is to find unique elements in the matrix i.e., those elements which are not repeated in the matrix or those elements whose frequency is 1. Examples: Input: mat[][] = [[2, 1, 4, 3], [1, 2, 3, 2], [3, 6, 2, 3], [5, 2, 5, 3]]Output: 4 6 Input
    5 min read
  • Sum of Matrix where each element is sum of row and column number
    Given two numbers M and N denoting the number of rows and columns of a matrix A[] where A[i][j] is the sum of i and j (indices follow 1 based indexing), the task is to find the sum of elements of the matrix. Examples: Input: M = 3, N = 3Output: 36Explanation: A[]: {{2, 3, 4}, {3, 4, 5}, {4, 5, 6}}.
    14 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
  • Enlarge a Matrix such that each element occurs in R rows and C columns
    Given a matrix arr[][] of size N x M, and two numbers R and C, the task is to enlarge this matrix such that each element of the original matrix occurs in R rows and C columns in the enlarged matrix. Examples: Input: arr[][] = {{1, 2, 3}, {4, 5, 6}} R = 3, C = 2 Output: 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2
    9 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 Su
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences