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++
// C++ program to find with maximum
// difference in a matrix
#include <bits/stdc++.h>
using namespace std;
#define N 4 // Rows
#define M 4 // Columns
// Function to find pair with maximum
// difference in a matrix
int maxDifferencePair(int mat[N][M])
{
int maxElement = INT_MIN; // max
int minElement = INT_MAX; // min
// Traverse the matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
// Find max element
if (mat[i][j] > maxElement) {
maxElement = mat[i][j];
}
// Find min element
if (mat[i][j] < minElement) {
minElement = mat[i][j];
}
}
}
return abs(maxElement - minElement);
}
// Driver Code
int main()
{
// matrix
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
// Java program to find with maximum
// difference in a matrix
import java.io.*;
class GFG {
static int N= 4; // Rows
static int M = 4; // Columns
// Function to find pair with maximum
// difference in a matrix
static int maxDifferencePair(int mat[][])
{
int maxElement = Integer.MIN_VALUE; // max
int minElement = Integer.MAX_VALUE; // min
// Traverse the matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
// Find max element
if (mat[i][j] > maxElement) {
maxElement = mat[i][j];
}
// Find min element
if (mat[i][j] < minElement) {
minElement = mat[i][j];
}
}
}
return Math.abs(maxElement - minElement);
}
// 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( maxDifferencePair(mat));
}
}
// This code is contributed by inder_verma..
Python3
# Python3 program to find with maximum
# difference in a matrix
N = 4 # Rows
M = 4 # Columns
# Function to find pair with maximum
# difference in a matrix
def maxDifferencePair(mat):
maxElement = -10**9 # max
minElement = 10**9 # min
# Traverse the matrix
for i in range(N):
for j in range(M):
# Find max element
if (mat[i][j] > maxElement):
maxElement = mat[i][j]
# Find min element
if (mat[i][j] < minElement):
minElement = mat[i][j]
return abs(maxElement - minElement)
# Driver Code
# matrix
mat = [[ 1, 2, 3, 4 ],
[ 25, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16]]
print(maxDifferencePair(mat))
# This code is contributed
# by mohit kumar
C#
// C# program to find with maximum
// difference in a matrix
using System;
class GFG
{
static int N = 4; // Rows
static int M = 4; // Columns
// Function to find pair with
// maximum difference in a matrix
static int maxDifferencePair(int [,]mat)
{
int maxElement = int.MinValue; // max
int minElement = int.MaxValue; // min
// Traverse the matrix
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
// Find max element
if (mat[i, j] > maxElement)
{
maxElement = mat[i, j];
}
// Find min element
if (mat[i, j] < minElement)
{
minElement = mat[i, j];
}
}
}
return Math.Abs(maxElement -
minElement);
}
// 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.WriteLine( maxDifferencePair(mat));
}
}
// This code is contributed
// by inder_verma
JavaScript
<script>
// JavaScript program to find with maximum
// difference in a matrix
let N= 4; // Rows
let M = 4; // Columns
// Function to find pair with maximum
// difference in a matrix
function maxDifferencePair(mat)
{
let maxElement = Number.MIN_VALUE; // max
let minElement = Number.MAX_VALUE; // min
// Traverse the matrix
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
// Find max element
if (mat[i][j] > maxElement) {
maxElement = mat[i][j];
}
// Find min element
if (mat[i][j] < minElement) {
minElement = mat[i][j];
}
}
}
return Math.abs(maxElement - minElement);
}
// Driver Code
let mat = [[ 1, 2, 3, 4 ],
[ 25, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16]];
document.write( maxDifferencePair(mat));
// This code is contributed by unknown2108
</script>
Similar Reads
Find pair with maximum difference in any column of a Matrix Given a N*N matrix of positive integers. The task is to find the maximum difference between any pair of elements in any column of the matrix. Examples: Input : mat[N][N] = { { 1, 2, 3, 4, 5 }, { 5, 3, 5, 4, 0 }, { 5, 6, 7, 8, 9 }, { 0, 6, 3, 4, 12 }, { 9, 7, 12, 4, 3 }, }; Output : Max difference :
6 min read
Pair with maximum sum in a Matrix Given a NxM matrix with N rows and M columns of positive integers. The task is to find the sum of pair with maximum sum in the matrix. Examples: Input : mat[N][M] = {{1, 2, 3, 4}, {25, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output : 41 Pair (25, 16) has the maximum sum Input : mat[N][M] = {{1,
7 min read
Find row with maximum sum in a Matrix Given an N*N matrix. The task is to find the index of a row with the maximum sum. That is the row whose sum of elements is 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 : Row 3 has max sum 35 Input :
11 min read
Find pair of rows in a binary matrix that has maximum bit difference Given a Binary Matrix. The task is to find the pair of row in the Binary matrix that has maximum bit difference Examples: Input: mat[][] = {{1, 1, 1, 1}, {1, 1, 0, 1}, {0, 0, 0, 0}}; Output : (1, 3) Bit difference between row numbers 1 and 3 is maximum with value 4. Bit difference between 1 and 2 is
15 min read
Maximum neighbor element in a matrix within distance K Given a matrix mat[][] and an integer K, the task is to find the maximum neighbor within an absolute distance of K for each element of the matrix.In other words for each Matrix[i][j] find maximum Matrix[p][q] such that abs (i-p) + abs (j-q) ? K.Examples:Â Â Input: mat[][] = {{1, 2}, {4, 5}}, K = 1Â Ou
8 min read