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 : 12
5th column has the pair with max difference
(0, 12)
Input : mat[N][N] = {
{ 1, 2, 3 },
{ 5, 3, 5 },
{ 9, 6, 7 }
};
Output : Max difference : 8
The idea is to observe that the pair with maximum difference in any column is the difference between the maximum and minimum element of that column. Now traverse the matrix column wise. Find the difference between the max and min element of every column and return the maximum difference.
Below is the implementation of the above approach:
C++
// C++ program to find column with // max difference of any pair of elements #include <bits/stdc++.h> using namespace std; #define N 5 // No of rows and column // Function to find the column with max difference int colMaxDiff(int mat[N][N]) { int max_diff = INT_MIN; // Traverse matrix column wise for (int i = 0; i < N; i++) { // Insert elements of column to vector int max_val = mat[0][i], min_val = mat[0][i]; for (int j = 1; j < N; j++) { max_val = max(max_val, mat[j][i]); min_val = min(min_val, mat[j][i]); } // calculating difference between maximum and // minimum max_diff = max(max_diff, max_val - min_val); } return max_diff; } // driver code int main() { int 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 }, }; cout << "Max difference : " << colMaxDiff(mat) << endl; return 0; } |
Java
// Java program to find column with // max difference of any pair of elements public class GOC1 { static int N = 5; // No of rows and column // Function to find the column with max difference static int colMaxDiff(int mat[][]) { int max_diff = Integer.MIN_VALUE; // Traverse matrix column wise for (int i = 0; i < N; i++) { // Insert elements of column to vector int max_val = mat[0][i], min_val = mat[0][i]; for (int j = 1; j < N; j++) { max_val = Math.max(max_val, mat[j][i]); min_val = Math.min(min_val, mat[j][i]); } // calculating difference between maximum and // minimum max_diff = Math.max(max_diff, max_val - min_val); } return max_diff; } // Driver Code public static void main(String args[]) { int mat[][] = { { 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 }, }; System.out.println("Max difference : "+colMaxDiff(mat)); } } |
Python3
# Python3 program to find column with # max difference of any pair of elements N = 5 # No of rows and column # Function to find the column # with max difference def colMaxDiff(mat): max_diff = 0 # Traverse matrix column wise for i in range (N): # Insert elements of column # to vector max_val = mat[0][i] min_val = mat[0][i] for j in range (1, N) : max_val = max(max_val, mat[j][i]) min_val = min(min_val, mat[j][i]) # calculating difference between # maximum and minimum max_diff = max(max_diff, max_val - min_val) return max_diff # Driver Code if __name__ == "__main__": mat = [[ 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 ]] print ("Max difference :", colMaxDiff(mat)) # This code is contributed by ita_c |
C#
// C# program to find column // with max difference of // any pair of elements using System; class GFG { static int N = 5; // No of rows and column // Function to find the column // with max difference static int colMaxDiff(int [,]mat) { int max_diff = int.MinValue; // Traverse matrix column wise for (int i = 0; i < N; i++) { // Insert elements of column // to vector int max_val = mat[0, i], min_val = mat[0, i]; for (int j = 1; j < N; j++) { max_val = Math.Max(max_val, mat[j, i]); min_val = Math.Min(min_val, mat[j, i]); } // calculating difference between // maximum and minimum max_diff = Math.Max(max_diff, max_val - min_val); } return max_diff; } // Driver Code public static void Main() { int [,]mat = { { 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 }}; Console.WriteLine("Max difference : " + colMaxDiff(mat)); } } // This code is contributed // by Subhadeep |
PHP
<?php // PHP program to find column with // max difference of any pair of elements // Function to find the column with max difference function colMaxDiff($mat) { $N = 5; $max_diff = PHP_INT_MIN; // Traverse matrix column wise for ($i = 0; $i < $N; $i++) { // Insert elements of column to vector $max_val = $mat[0][$i]; $min_val = $mat[0][$i]; for ($j = 1; $j < $N; $j++) { $max_val = max($max_val, $mat[$j][$i]); $min_val = min($min_val, $mat[$j][$i]); } // calculating difference between maximum and // minimum $max_diff = max($max_diff, $max_val - $min_val); } return $max_diff; } // driver code $mat = array(array( 1, 2, 3, 4, 5 ), array(5, 3, 5, 4, 0 ), array(5, 6, 7, 8, 9 ), array(0, 6, 3, 4, 12 ), array(9, 7, 12, 4, 3 )); echo "Max difference : " . colMaxDiff($mat) . "\n"; // This code is contributed // by Akanksha Rai |
Max difference : 12
Time Complexity: O(n^2)
Recommended Posts:
- Find pair of rows in a binary matrix that has maximum bit difference
- Pair with maximum difference in a Matrix
- Find column with maximum sum in a Matrix
- Find maximum element of each column in a matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Replace every matrix element with maximum of GCD of row or column
- Check if a pair with given absolute difference exists in a Matrix
- Pair with maximum sum in a Matrix
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Count ways of choosing a pair with maximum difference
- Program to find the Sum of each Row and each Column of a Matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Find if a binary matrix exists with given row and column sums
- Find the original matrix when largest element in a row and a column are given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



