Program to find the maximum element in a Matrix
Last Updated :
06 Dec, 2022
Given an 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++
#include <bits/stdc++.h>
using namespace std;
#define N 4
#define M 4
int findMax( int mat[N][M])
{
int maxElement = INT_MIN;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (mat[i][j] > maxElement) {
maxElement = mat[i][j];
}
}
}
return maxElement;
}
int main()
{
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
public class GFG {
final static int N = 4 ;
final static int M = 4 ;
static int findMax( int mat[][])
{
int maxElement = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < M; j++) {
if (mat[i][j] > maxElement) {
maxElement = mat[i][j];
}
}
}
return maxElement;
}
public static void main(String args[])
{
int mat[][] = { { 1 , 2 , 3 , 4 },
{ 25 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
System.out.println(findMax(mat)) ;
}
}
|
Python3
import sys
N = 4
M = 4
def findMax(mat):
maxElement = - sys.maxsize - 1
for i in range (N):
for j in range (M):
if (mat[i][j] > maxElement):
maxElement = mat[i][j]
return maxElement
if __name__ = = '__main__' :
mat = [[ 1 , 2 , 3 , 4 ],
[ 25 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 ]]
print (findMax(mat))
|
C#
using System;
class GFG {
static int N = 4;
static int M = 4 ;
static int findMax( int [,] mat)
{
int maxElement = int .MinValue;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (mat[i,j] > maxElement) {
maxElement = mat[i,j];
}
}
}
return maxElement;
}
public static void Main()
{
int [,]mat = {{ 1, 2, 3, 4},
{25, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
Console.Write(findMax(mat)) ;
}
}
|
PHP
<?php
function findMax( $mat )
{
$maxElement = PHP_INT_MIN;
for ( $i = 0; $i < 4; $i ++)
{
for ( $j = 0; $j < 4; $j ++)
{
if ( $mat [ $i ][ $j ] > $maxElement )
{
$maxElement = $mat [ $i ][ $j ];
}
}
}
return $maxElement ;
}
$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" ;
?>
|
Javascript
<script>
let N = 4;
let M = 4 ;
function findMax(mat)
{
let maxElement = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
if (mat[i][j] > maxElement) {
maxElement = mat[i][j];
}
}
}
return maxElement;
}
let mat = [[ 1, 2, 3, 4 ],
[ 25, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ]];
document.write(findMax(mat)) ;
</script>
|
Time Complexity: O(N*M), where N and M are the numbers of rows and columns of the given matrix.
Auxiliary Space: O(1), As constant extra space is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...