Find pair with maximum difference in any column of a Matrix
Last Updated :
01 Nov, 2022
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 a 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 elements of every column and return the maximum difference.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 5 // No of rows and column
int colMaxDiff( int mat[N][N])
{
int max_diff = INT_MIN;
for ( int i = 0; i < N; i++) {
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]);
}
max_diff = max(max_diff, max_val - min_val);
}
return max_diff;
}
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
public class GOC1 {
static int N = 5 ;
static int colMaxDiff( int mat[][]) {
int max_diff = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
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]);
}
max_diff = Math.max(max_diff, max_val - min_val);
}
return max_diff;
}
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
N = 5
def colMaxDiff(mat):
max_diff = 0
for i in range (N):
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])
max_diff = max (max_diff, max_val -
min_val)
return max_diff
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))
|
C#
using System;
class GFG
{
static int N = 5;
static int colMaxDiff( int [,]mat)
{
int max_diff = int .MinValue;
for ( int i = 0; i < N; i++)
{
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]);
}
max_diff = Math.Max(max_diff,
max_val - min_val);
}
return max_diff;
}
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));
}
}
|
PHP
<?php
function colMaxDiff( $mat )
{
$N = 5;
$max_diff = PHP_INT_MIN;
for ( $i = 0; $i < $N ; $i ++)
{
$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 ]);
}
$max_diff = max( $max_diff , $max_val - $min_val );
}
return $max_diff ;
}
$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" ;
|
Javascript
<script>
let N = 5;
function colMaxDiff(mat)
{
let max_diff = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
let max_val = mat[0][i], min_val = mat[0][i];
for (let j = 1; j < N; j++) {
max_val = Math.max(max_val, mat[j][i]);
min_val = Math.min(min_val, mat[j][i]);
}
max_diff = Math.max(max_diff, max_val - min_val);
}
return max_diff;
}
let 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 ]];
document.write( "Max difference :" + colMaxDiff(mat))
</script>
|
Output
Max difference : 12
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...