Pair with maximum difference in a Matrix
Last Updated :
05 Sep, 2022
Given a NxM matrix with N rows and M columns of positive integers. The task is to find the pair with the maximum difference in the given matrix.
Note: Pairs at positions (a, b) and (b, a) are considered equivalent.
Examples:
Input : mat[N][M] = {{1, 2, 3, 4},
{25, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}}
Output : 24
Pair (25, 1) has the maximum difference
Input : mat[N][M] = {{1, 2, 3},
{4, 6, 7},
{9, 10, 5}}
Output : 9
Pair (10, 1) has the maximum difference.
The idea is to observe that the elements contributing to the pair with maximum difference are the maximum and minimum elements in the matrix. So, find the maximum and minimum elements in the matrix and return the difference between them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 4 // Rows
#define M 4 // Columns
int maxDifferencePair( int mat[N][M])
{
int maxElement = INT_MIN;
int minElement = INT_MAX;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (mat[i][j] > maxElement) {
maxElement = mat[i][j];
}
if (mat[i][j] < minElement) {
minElement = mat[i][j];
}
}
}
return abs (maxElement - minElement);
}
int main()
{
int mat[N][M] = { { 1, 2, 3, 4 },
{ 25, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
cout << maxDifferencePair(mat) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int N= 4 ;
static int M = 4 ;
static int maxDifferencePair( int mat[][])
{
int maxElement = Integer.MIN_VALUE;
int minElement = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < M; j++) {
if (mat[i][j] > maxElement) {
maxElement = mat[i][j];
}
if (mat[i][j] < minElement) {
minElement = mat[i][j];
}
}
}
return Math.abs(maxElement - minElement);
}
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( maxDifferencePair(mat));
}
}
|
Python3
N = 4
M = 4
def maxDifferencePair(mat):
maxElement = - 10 * * 9
minElement = 10 * * 9
for i in range (N):
for j in range (M):
if (mat[i][j] > maxElement):
maxElement = mat[i][j]
if (mat[i][j] < minElement):
minElement = mat[i][j]
return abs (maxElement - minElement)
mat = [[ 1 , 2 , 3 , 4 ],
[ 25 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 ]]
print (maxDifferencePair(mat))
|
C#
using System;
class GFG
{
static int N = 4;
static int M = 4;
static int maxDifferencePair( int [,]mat)
{
int maxElement = int .MinValue;
int minElement = int .MaxValue;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
if (mat[i, j] > maxElement)
{
maxElement = mat[i, j];
}
if (mat[i, j] < minElement)
{
minElement = mat[i, j];
}
}
}
return Math.Abs(maxElement -
minElement);
}
public static void Main ()
{
int [,]mat = {{ 1, 2, 3, 4 },
{ 25, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 }};
Console.WriteLine( maxDifferencePair(mat));
}
}
|
Javascript
<script>
let N= 4;
let M = 4;
function maxDifferencePair(mat)
{
let maxElement = Number.MIN_VALUE;
let minElement = Number.MAX_VALUE;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
if (mat[i][j] > maxElement) {
maxElement = mat[i][j];
}
if (mat[i][j] < minElement) {
minElement = mat[i][j];
}
}
}
return Math.abs(maxElement - minElement);
}
let mat = [[ 1, 2, 3, 4 ],
[ 25, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16]];
document.write( maxDifferencePair(mat));
</script>
|
Share your thoughts in the comments
Please Login to comment...