Program to find the maximum element in a Matrix - GeeksforGeeks

07 May, 2021
Given a NxM matrix. The task is to find the maximum element in this matrix.Examples:  
Input: mat[4][4] = {{1, 2, 3, 4},
                    {25, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 16}};
Output: 25

Input: mat[3][4] = {{9, 8, 7, 6},
                    {5, 4, 3, 2},
                    {1, 0, 12, 45}};
Output: 45
 
Approach: The idea is to traverse the matrix using two nested loops, one for rows and one for columns and find the maximum element. Initialize a variable maxElement with a minimum value and traverse the matrix and compare every time if the current element is greater than a maxElement. If yes then update maxElement with the current element.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP code to find max element in a matrix#include <bits/stdc++.h>using namespace std; #define N 4#define M 4 // Function to find max element// mat[][] : 2D array to find max elementint findMax(int mat[N][M]){     // Initializing max element as INT_MIN    int maxElement = INT_MIN;     // checking each element of matrix    // if it is greater than maxElement,    // update maxElement    for (int i = 0; i < N; i++) {        for (int j = 0; j < M; j++) {            if (mat[i][j] > maxElement) {                maxElement = mat[i][j];            }        }    }     // finally return maxElement    return maxElement;} // Driver codeint main(){     // matrix    int mat[N][M] = { { 1, 2, 3, 4 },                      { 25, 6, 7, 8 },                      { 9, 10, 11, 12 },                      { 13, 14, 15, 16 } };     cout << findMax(mat) << endl;     return 0;}
// Java code to find max element in a matrix public class GFG {         final static int N = 4;    final static int  M = 4 ;     // Function to find max element    // mat[][] : 2D array to find max element    static int findMax(int mat[][])    {         // Initializing max element as INT_MIN        int maxElement = Integer.MIN_VALUE;         // checking each element of matrix        // if it is greater than maxElement,        // update maxElement        for (int i = 0; i < N; i++) {            for (int j = 0; j < M; j++) {                if (mat[i][j] > maxElement) {                    maxElement = mat[i][j];                }            }        }         // finally return maxElement        return maxElement;    }          // Driver code    public static void main(String args[])    {           // matrix        int mat[][] = { { 1, 2, 3, 4 },                          { 25, 6, 7, 8 },                          { 9, 10, 11, 12 },                          { 13, 14, 15, 16 } };         System.out.println(findMax(mat)) ;       }    // This Code is contributed by ANKITRAI1}
# Python 3 code to find max element# in a matriximport sysN = 4M = 4 # Function to find max element# mat[][] : 2D array to find max elementdef findMax(mat):         # Initializing max element as INT_MIN    maxElement = -sys.maxsize - 1     # checking each element of matrix    # if it is greater than maxElement,    # update maxElement    for i in range(N):        for j in range(M):            if (mat[i][j] > maxElement):                maxElement = mat[i][j]             # finally return maxElement    return maxElement # Driver codeif __name__ == '__main__':         # matrix    mat = [[1, 2, 3, 4],           [25, 6, 7, 8],           [9, 10, 11, 12],           [13, 14, 15, 16]]    print(findMax(mat)) # This code is contributed by# Surendra_Gangwar
// C# code to find max element in a matrixusing System; class GFG {         static int N = 4;    static int M = 4 ;     // Function to find max element    // mat[,] : 2D array to find max element    static int findMax(int[,] mat)    {         // Initializing max element as INT_MIN        int maxElement = int.MinValue;         // checking each element of matrix        // if it is greater than maxElement,        // update maxElement        for (int i = 0; i < N; i++) {                         for (int j = 0; j < M; j++) {                                 if (mat[i,j] > maxElement) {                                         maxElement = mat[i,j];                }            }        }         // finally return maxElement        return maxElement;    }          // Driver code    public static void Main()    {                 // matrix        int[,]mat = {{ 1, 2, 3, 4},                     {25, 6, 7, 8},                     {9, 10, 11, 12},                     {13, 14, 15, 16}};         Console.Write(findMax(mat)) ;    }     } // This code is contributed by ChitraNayal
<?php// PHP code to find max element in a matrix // Function to find max element// mat[][] : 2D array to find max elementfunction findMax($mat){     // Initializing max element as INT_MIN    $maxElement = PHP_INT_MIN;     // checking each element of matrix    // if it is greater than maxElement,    // update maxElement    for ($i = 0; $i < 4; $i++)    {        for ($j = 0; $j < 4; $j++)        {            if ($mat[$i][$j] > $maxElement)            {                $maxElement = $mat[$i][$j];            }        }    }     // finally return maxElement    return $maxElement;} // Driver code$mat = array(array(1, 2, 3, 4),             array(25, 6, 7, 8),             array(9, 10, 11, 12),             array(13, 14, 15, 16)); echo findMax($mat) . "\n"; // This code is contributed// by Akanksha Rai?>
<script>// Java script code to find max element in a matrix let N = 4;    let M = 4 ;     // Function to find max element    // mat[][] : 2D array to find max element    function findMax(mat)    {         // Initializing max element as INT_MIN        let maxElement = Number.MIN_VALUE;         // checking each element of matrix        // if it is greater than maxElement,        // update maxElement        for (let i = 0; i < N; i++) {            for (let j = 0; j < M; j++) {                if (mat[i][j] > maxElement) {                    maxElement = mat[i][j];                }            }        }         // finally return maxElement        return maxElement;    }          // Driver code             // matrix        let mat = [[ 1, 2, 3, 4 ],                        [ 25, 6, 7, 8 ],                        [ 9, 10, 11, 12 ],                        [ 13, 14, 15, 16 ]];         document.write(findMax(mat)) ; // This code is contributed by manoj</script>
25
 
Time Complexity: O(N*M) 
ankthon
ukasp
SURENDRA_GANGWAR
Akanksha_Rai
manojkumarreddymallidi
Matrix
School Programming
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sudoku | Backtracking-7
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Count all possible paths from top left to bottom right of a mXn matrix
Program to multiply two matrices
Min Cost Path | DP-6
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects