Pair with maximum difference in a Matrix - GeeksforGeeks

28 May, 2021
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++
Java
Python3
C#
Javascript
// 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 matrixint 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 Codeint 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 program to find with maximum// difference in a matrix import java.io.*; class GFG {  static int N= 4; // Rowsstatic int  M = 4; // Columns // Function to find pair with maximum// difference in a matrixstatic 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 program to find with maximum# difference in a matrix N = 4 # RowsM = 4 # Columns # Function to find pair with maximum# difference in a matrixdef 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 # matrixmat = [[ 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# program to find with maximum// difference in a matrixusing System; class GFG{static int N = 4; // Rowsstatic int M = 4; // Columns // Function to find pair with// maximum difference in a matrixstatic 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 Codepublic 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
<script> // JavaScript program to find with maximum// difference in a matrix let N= 4; // Rowslet M = 4; // Columns // Function to find pair with maximum// difference in a matrixfunction 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 Codelet 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>
24
 
inderDuMCA
mohit kumar 29
unknown2108
Arrays
Matrix
Arrays
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Program to multiply two matrices
Min Cost Path | DP-6
Printing all solutions in N-Queen Problem
The Celebrity Problem
Rotate a matrix by 90 degree in clockwise direction without using any extra space
Gold Mine Problem
Python program to multiply two matrices
Multiplication of two Matrices in Single line using Numpy in Python
Program to find transpose of a matrix