Given an n x n matrix mat[n][n] of integers, find the maximum value of mat(c, d) – mat(a, b) over all choices of indexes such that both c > a and d > b.
Example:
Input:
mat[N][N] = {{ 1, 2, -1, -4, -20 },
{ -8, -3, 4, 2, 1 },
{ 3, 8, 6, 1, 3 },
{ -4, -1, 1, 7, -6 },
{ 0, -4, 10, -5, 1 }};
Output: 18
The maximum value is 18 as mat[4][2]
- mat[1][0] = 18 has maximum difference.
The program should do only ONE traversal of the matrix. i.e. expected time complexity is O(n2)
A simple solution would be to apply Brute-Force. For all values mat(a, b) in the matrix, we find mat(c, d) that has maximum value such that c > a and d > b and keeps on updating maximum value found so far. We finally return the maximum value.
Below is its implementation.
C++
#include <bits/stdc++.h>
using namespace std;
#define N 5
int findMaxValue( int mat[][N])
{
int maxValue = INT_MIN;
for ( int a = 0; a < N - 1; a++)
for ( int b = 0; b < N - 1; b++)
for ( int d = a + 1; d < N; d++)
for ( int e = b + 1; e < N; e++)
if (maxValue < (mat[d][e] - mat[a][b]))
maxValue = mat[d][e] - mat[a][b];
return maxValue;
}
int main()
{
int mat[N][N] = {
{ 1, 2, -1, -4, -20 },
{ -8, -3, 4, 2, 1 },
{ 3, 8, 6, 1, 3 },
{ -4, -1, 1, 7, -6 },
{ 0, -4, 10, -5, 1 }
};
cout << "Maximum Value is "
<< findMaxValue(mat);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int findMaxValue( int N, int mat[][])
{
int maxValue = Integer.MIN_VALUE;
for ( int a = 0 ; a < N - 1 ; a++)
for ( int b = 0 ; b < N - 1 ; b++)
for ( int d = a + 1 ; d < N; d++)
for ( int e = b + 1 ; e < N; e++)
if (maxValue < (mat[d][e] - mat[a][b]))
maxValue = mat[d][e] - mat[a][b];
return maxValue;
}
public static void main (String[] args)
{
int N = 5 ;
int mat[][] = {
{ 1 , 2 , - 1 , - 4 , - 20 },
{ - 8 , - 3 , 4 , 2 , 1 },
{ 3 , 8 , 6 , 1 , 3 },
{ - 4 , - 1 , 1 , 7 , - 6 },
{ 0 , - 4 , 10 , - 5 , 1 }
};
System.out.print( "Maximum Value is " +
findMaxValue(N,mat));
}
}
|
Python 3
N = 5
def findMaxValue(mat):
maxValue = 0
for a in range (N - 1 ):
for b in range (N - 1 ):
for d in range (a + 1 , N):
for e in range (b + 1 , N):
if maxValue < int (mat[d][e] -
mat[a][b]):
maxValue = int (mat[d][e] -
mat[a][b]);
return maxValue;
mat = [[ 1 , 2 , - 1 , - 4 , - 20 ],
[ - 8 , - 3 , 4 , 2 , 1 ],
[ 3 , 8 , 6 , 1 , 3 ],
[ - 4 , - 1 , 1 , 7 , - 6 ],
[ 0 , - 4 , 10 , - 5 , 1 ]];
print ( "Maximum Value is " +
str (findMaxValue(mat)))
|
C#
using System;
class GFG
{
static int findMaxValue( int N,
int [,]mat)
{
int maxValue = int .MinValue;
for ( int a = 0; a< N - 1; a++)
for ( int b = 0; b < N - 1; b++)
for ( int d = a + 1; d < N; d++)
for ( int e = b + 1; e < N; e++)
if (maxValue < (mat[d, e] -
mat[a, b]))
maxValue = mat[d, e] -
mat[a, b];
return maxValue;
}
public static void Main ()
{
int N = 5;
int [,]mat = {{1, 2, -1, -4, -20},
{-8, -3, 4, 2, 1},
{3, 8, 6, 1, 3},
{-4, -1, 1, 7, -6},
{0, -4, 10, -5, 1}};
Console.Write( "Maximum Value is " +
findMaxValue(N,mat));
}
}
|
Javascript
<script>
function findMaxValue(N,mat)
{
let maxValue = Number.MIN_VALUE;
for (let a = 0; a < N - 1; a++)
for (let b = 0; b < N - 1; b++)
for (let d = a + 1; d < N; d++)
for (let e = b + 1; e < N; e++)
if (maxValue < (mat[d][e] - mat[a][b]))
maxValue = mat[d][e] - mat[a][b];
return maxValue;
}
let N = 5;
let mat=[[ 1, 2, -1, -4, -20],[-8, -3, 4, 2, 1],[3, 8, 6, 1, 3],[ -4, -1, 1, 7, -6 ],[ 0, -4, 10, -5, 1 ]];
document.write( "Maximum Value is " +findMaxValue(N,mat));
</script>
|
PHP
<?php
$N = 5;
function findMaxValue(& $mat )
{
global $N ;
$maxValue = PHP_INT_MIN;
for ( $a = 0; $a < $N - 1; $a ++)
for ( $b = 0; $b < $N - 1; $b ++)
for ( $d = $a + 1; $d < $N ; $d ++)
for ( $e = $b + 1; $e < $N ; $e ++)
if ( $maxValue < ( $mat [ $d ][ $e ] -
$mat [ $a ][ $b ]))
$maxValue = $mat [ $d ][ $e ] -
$mat [ $a ][ $b ];
return $maxValue ;
}
$mat = array ( array (1, 2, -1, -4, -20),
array (-8, -3, 4, 2, 1),
array (3, 8, 6, 1, 3),
array (-4, -1, 1, 7, -6),
array (0, -4, 10, -5, 1));
echo "Maximum Value is " .
findMaxValue( $mat );
?>
|
Output
Maximum Value is 18
Time complexity: O(N4).
Auxiliary Space: O(1)
The above program runs in O(n^4) time which is nowhere close to expected time complexity of O(n^2)
An efficient solution uses extra space. We pre-process the matrix such that index(i, j) stores max of elements in matrix from (i, j) to (N-1, N-1) and in the process keeps on updating maximum value found so far. We finally return the maximum value.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 5
int findMaxValue( int mat[][N])
{
int maxValue = INT_MIN;
int maxArr[N][N];
maxArr[N-1][N-1] = mat[N-1][N-1];
int maxv = mat[N-1][N-1];
for ( int j = N - 2; j >= 0; j--)
{
if (mat[N-1][j] > maxv)
maxv = mat[N - 1][j];
maxArr[N-1][j] = maxv;
}
maxv = mat[N - 1][N - 1];
for ( int i = N - 2; i >= 0; i--)
{
if (mat[i][N - 1] > maxv)
maxv = mat[i][N - 1];
maxArr[i][N - 1] = maxv;
}
for ( int i = N-2; i >= 0; i--)
{
for ( int j = N-2; j >= 0; j--)
{
if (maxArr[i+1][j+1] - mat[i][j] >
maxValue)
maxValue = maxArr[i + 1][j + 1] - mat[i][j];
maxArr[i][j] = max(mat[i][j],
max(maxArr[i][j + 1],
maxArr[i + 1][j]) );
}
}
return maxValue;
}
int main()
{
int mat[N][N] = {
{ 1, 2, -1, -4, -20 },
{ -8, -3, 4, 2, 1 },
{ 3, 8, 6, 1, 3 },
{ -4, -1, 1, 7, -6 },
{ 0, -4, 10, -5, 1 }
};
cout << "Maximum Value is "
<< findMaxValue(mat);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int findMaxValue( int N, int mat[][])
{
int maxValue = Integer.MIN_VALUE;
int maxArr[][] = new int [N][N];
maxArr[N- 1 ][N- 1 ] = mat[N- 1 ][N- 1 ];
int maxv = mat[N- 1 ][N- 1 ];
for ( int j = N - 2 ; j >= 0 ; j--)
{
if (mat[N- 1 ][j] > maxv)
maxv = mat[N - 1 ][j];
maxArr[N- 1 ][j] = maxv;
}
maxv = mat[N - 1 ][N - 1 ];
for ( int i = N - 2 ; i >= 0 ; i--)
{
if (mat[i][N - 1 ] > maxv)
maxv = mat[i][N - 1 ];
maxArr[i][N - 1 ] = maxv;
}
for ( int i = N- 2 ; i >= 0 ; i--)
{
for ( int j = N- 2 ; j >= 0 ; j--)
{
if (maxArr[i+ 1 ][j+ 1 ] - mat[i][j] > maxValue)
maxValue = maxArr[i + 1 ][j + 1 ] - mat[i][j];
maxArr[i][j] = Math.max(mat[i][j],
Math.max(maxArr[i][j + 1 ],
maxArr[i + 1 ][j]) );
}
}
return maxValue;
}
public static void main (String[] args)
{
int N = 5 ;
int mat[][] = {
{ 1 , 2 , - 1 , - 4 , - 20 },
{ - 8 , - 3 , 4 , 2 , 1 },
{ 3 , 8 , 6 , 1 , 3 },
{ - 4 , - 1 , 1 , 7 , - 6 },
{ 0 , - 4 , 10 , - 5 , 1 }
};
System.out.print( "Maximum Value is " +
findMaxValue(N,mat));
}
}
|
Python3
import sys
N = 5
def findMaxValue(mat):
maxValue = - sys.maxsize - 1
maxArr = [[ 0 for x in range (N)]
for y in range (N)]
maxArr[N - 1 ][N - 1 ] = mat[N - 1 ][N - 1 ]
maxv = mat[N - 1 ][N - 1 ];
for j in range (N - 2 , - 1 , - 1 ):
if (mat[N - 1 ][j] > maxv):
maxv = mat[N - 1 ][j]
maxArr[N - 1 ][j] = maxv
maxv = mat[N - 1 ][N - 1 ]
for i in range (N - 2 , - 1 , - 1 ):
if (mat[i][N - 1 ] > maxv):
maxv = mat[i][N - 1 ]
maxArr[i][N - 1 ] = maxv
for i in range (N - 2 , - 1 , - 1 ):
for j in range (N - 2 , - 1 , - 1 ):
if (maxArr[i + 1 ][j + 1 ] -
mat[i][j] > maxValue):
maxValue = (maxArr[i + 1 ][j + 1 ] -
mat[i][j])
maxArr[i][j] = max (mat[i][j],
max (maxArr[i][j + 1 ],
maxArr[i + 1 ][j]))
return maxValue
mat = [[ 1 , 2 , - 1 , - 4 , - 20 ],
[ - 8 , - 3 , 4 , 2 , 1 ],
[ 3 , 8 , 6 , 1 , 3 ],
[ - 4 , - 1 , 1 , 7 , - 6 ] ,
[ 0 , - 4 , 10 , - 5 , 1 ]]
print ( "Maximum Value is" ,
findMaxValue(mat))
|
C#
using System;
class GFG {
static int findMaxValue( int N, int [,]mat)
{
int maxValue = int .MinValue;
int [,]maxArr = new int [N, N];
maxArr[N - 1, N - 1] = mat[N - 1,N - 1];
int maxv = mat[N - 1, N - 1];
for ( int j = N - 2; j >= 0; j--)
{
if (mat[N - 1, j] > maxv)
maxv = mat[N - 1, j];
maxArr[N - 1, j] = maxv;
}
maxv = mat[N - 1,N - 1];
for ( int i = N - 2; i >= 0; i--)
{
if (mat[i, N - 1] > maxv)
maxv = mat[i,N - 1];
maxArr[i,N - 1] = maxv;
}
for ( int i = N - 2; i >= 0; i--)
{
for ( int j = N - 2; j >= 0; j--)
{
if (maxArr[i + 1,j + 1] -
mat[i, j] > maxValue)
maxValue = maxArr[i + 1,j + 1] -
mat[i, j];
maxArr[i,j] = Math.Max(mat[i, j],
Math.Max(maxArr[i, j + 1],
maxArr[i + 1, j]) );
}
}
return maxValue;
}
public static void Main ()
{
int N = 5;
int [,]mat = {{ 1, 2, -1, -4, -20 },
{ -8, -3, 4, 2, 1 },
{ 3, 8, 6, 1, 3 },
{ -4, -1, 1, 7, -6 },
{ 0, -4, 10, -5, 1 }};
Console.Write( "Maximum Value is " +
findMaxValue(N,mat));
}
}
|
Javascript
<script>
function findMaxValue(N,mat)
{
let maxValue = Number.MIN_VALUE;
let maxArr= new Array(N);
for (let i = 0; i < N; i++)
{
maxArr[i]= new Array(N);
}
maxArr[N - 1][N - 1] = mat[N - 1][N - 1];
let maxv = mat[N-1][N-1];
for (let j = N - 2; j >= 0; j--)
{
if (mat[N - 1][j] > maxv)
maxv = mat[N - 1][j];
maxArr[N - 1][j] = maxv;
}
maxv = mat[N - 1][N - 1];
for (let i = N - 2; i >= 0; i--)
{
if (mat[i][N - 1] > maxv)
maxv = mat[i][N - 1];
maxArr[i][N - 1] = maxv;
}
for (let i = N-2; i >= 0; i--)
{
for (let j = N-2; j >= 0; j--)
{
if (maxArr[i+1][j+1] - mat[i][j] > maxValue)
maxValue = maxArr[i + 1][j + 1] - mat[i][j];
maxArr[i][j] = Math.max(mat[i][j],
Math.max(maxArr[i][j + 1],
maxArr[i + 1][j]) );
}
}
return maxValue;
}
let N = 5;
let mat = [[ 1, 2, -1, -4, -20 ],
[-8, -3, 4, 2, 1 ],
[ 3, 8, 6, 1, 3 ],
[ -4, -1, 1, 7, -6] ,
[0, -4, 10, -5, 1 ]];
document.write( "Maximum Value is " +
findMaxValue(N,mat));
</script>
|
PHP
<?php
$N = 5;
function findMaxValue( $mat )
{
global $N ;
$maxValue = PHP_INT_MIN;
$maxArr [ $N ][ $N ] = array ();
$maxArr [ $N - 1][ $N - 1] = $mat [ $N - 1][ $N - 1];
$maxv = $mat [ $N - 1][ $N - 1];
for ( $j = $N - 2; $j >= 0; $j --)
{
if ( $mat [ $N - 1][ $j ] > $maxv )
$maxv = $mat [ $N - 1][ $j ];
$maxArr [ $N - 1][ $j ] = $maxv ;
}
$maxv = $mat [ $N - 1][ $N - 1];
for ( $i = $N - 2; $i >= 0; $i --)
{
if ( $mat [ $i ][ $N - 1] > $maxv )
$maxv = $mat [ $i ][ $N - 1];
$maxArr [ $i ][ $N - 1] = $maxv ;
}
for ( $i = $N - 2; $i >= 0; $i --)
{
for ( $j = $N - 2; $j >= 0; $j --)
{
if ( $maxArr [ $i + 1][ $j + 1] -
$mat [ $i ][ $j ] > $maxValue )
$maxValue = $maxArr [ $i + 1][ $j + 1] -
$mat [ $i ][ $j ];
$maxArr [ $i ][ $j ] = max( $mat [ $i ][ $j ],
max( $maxArr [ $i ][ $j + 1],
$maxArr [ $i + 1][ $j ]));
}
}
return $maxValue ;
}
$mat = array ( array (1, 2, -1, -4, -20),
array (-8, -3, 4, 2, 1),
array (3, 8, 6, 1, 3),
array (-4, -1, 1, 7, -6),
array (0, -4, 10, -5, 1)
);
echo "Maximum Value is " .
findMaxValue( $mat );
?>
|
Output
Maximum Value is 18
Time complexity: O(N2).
Auxiliary Space: O(N2)
If we are allowed to modify of the matrix, we can avoid using extra space and use input matrix instead.
Exercise: Print index (a, b) and (c, d) as well.
An optimal approach is with space complexity O(N).
Instead of using the maxArr matrix, we can use two separate vectors (temp1 and temp2) to get maxArr[i+1][j] and maxArr[i][j+1] values.
C++
#include <bits/stdc++.h>
using namespace std;
#define N 5
int findMaxValue( int mat[][N])
{
vector< int > temp1(N), temp2(N);
temp1[N - 1] = mat[N - 1][N - 1];
for ( int j = N - 2; j >= 0; j--)
temp1[j] = max(temp1[j + 1], mat[N - 1][j]);
int maxValue = INT_MIN;
for ( int i = N - 2; i >= 0; i--) {
temp2[N - 1] = max(temp1[N - 1], mat[i][N - 1]);
for ( int j = N - 2; j >= 0; j--) {
maxValue
= max(maxValue, temp1[j + 1] - mat[i][j]);
temp2[j] = max(
{ mat[i][j], temp1[j], temp2[j + 1] });
}
temp1 = temp2;
}
return maxValue;
}
int main()
{
int mat[N][N] = { { 1, 2, -1, -4, -20 },
{ -8, -3, 4, 2, 1 },
{ 3, 8, 6, 1, 3 },
{ -4, -1, 1, 7, -6 },
{ 0, -4, 10, -5, 1 } };
cout << "Maximum Value is " << findMaxValue(mat);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static final int N = 5 ;
static int findMaxValue( int [][] mat) {
int [] temp1 = new int [N];
int [] temp2 = new int [N];
temp1[N - 1 ] = mat[N - 1 ][N - 1 ];
for ( int j = N - 2 ; j >= 0 ; j--) {
temp1[j] = Math.max(temp1[j + 1 ], mat[N - 1 ][j]);
}
int maxValue = Integer.MIN_VALUE;
for ( int i = N - 2 ; i >= 0 ; i--) {
temp2[N - 1 ] = Math.max(temp1[N - 1 ], mat[i][N - 1 ]);
for ( int j = N - 2 ; j >= 0 ; j--) {
maxValue = Math.max(maxValue, temp1[j + 1 ] - mat[i][j]);
temp2[j] = Math.max(mat[i][j], Math.max(temp1[j], temp2[j + 1 ]));
}
temp1 = Arrays.copyOf(temp2, temp2.length);
}
return maxValue;
}
public static void main(String[] args) {
int [][] mat = {
{ 1 , 2 , - 1 , - 4 , - 20 },
{ - 8 , - 3 , 4 , 2 , 1 },
{ 3 , 8 , 6 , 1 , 3 },
{ - 4 , - 1 , 1 , 7 , - 6 },
{ 0 , - 4 , 10 , - 5 , 1 }
};
System.out.println( "Maximum Value is " + findMaxValue(mat));
}
}
|
Python3
def find_max_value(mat):
N = len (mat)
temp1 = [ 0 ] * N
temp2 = [ 0 ] * N
temp1[N - 1 ] = mat[N - 1 ][N - 1 ]
for j in range (N - 2 , - 1 , - 1 ):
temp1[j] = max (temp1[j + 1 ], mat[N - 1 ][j])
max_value = float ( "-inf" )
for i in range (N - 2 , - 1 , - 1 ):
temp2[N - 1 ] = max (temp1[N - 1 ], mat[i][N - 1 ])
for j in range (N - 2 , - 1 , - 1 ):
max_value = max (max_value, temp1[j + 1 ] - mat[i][j])
temp2[j] = max (mat[i][j], temp1[j], temp2[j + 1 ])
temp1 = temp2
return max_value
if __name__ = = "__main__" :
mat = [[ 1 , 2 , - 1 , - 4 , - 20 ],
[ - 8 , - 3 , 4 , 2 , 1 ],
[ 3 , 8 , 6 , 1 , 3 ],
[ - 4 , - 1 , 1 , 7 , - 6 ],
[ 0 , - 4 , 10 , - 5 , 1 ]]
print ( "Maximum Value is" , find_max_value(mat))
|
C#
using System;
class Program
{
const int N = 5;
static int FindMaxValue( int [,] mat)
{
int [] temp1 = new int [N];
int [] temp2 = new int [N];
temp1[N - 1] = mat[N - 1, N - 1];
for ( int j = N - 2; j >= 0; j--)
temp1[j] = Math.Max(temp1[j + 1], mat[N - 1, j]);
int maxValue = int .MinValue;
for ( int i = N - 2; i >= 0; i--)
{
temp2[N - 1] = Math.Max(temp1[N - 1], mat[i, N - 1]);
for ( int j = N - 2; j >= 0; j--)
{
maxValue = Math.Max(maxValue, temp1[j + 1] - mat[i, j]);
temp2[j] = Math.Max(mat[i, j], Math.Max(temp1[j], temp2[j + 1]));
}
temp1 = ( int [])temp2.Clone();
}
return maxValue;
}
static void Main()
{
int [,] mat = { { 1, 2, -1, -4, -20 },
{ -8, -3, 4, 2, 1 },
{ 3, 8, 6, 1, 3 },
{ -4, -1, 1, 7, -6 },
{ 0, -4, 10, -5, 1 } };
Console.WriteLine( "Maximum Value is " + FindMaxValue(mat));
}
}
|
Javascript
const N = 5;
function findMaxValue(mat) {
const temp1 = new Array(N);
const temp2 = new Array(N);
temp1[N - 1] = mat[N - 1][N - 1];
for (let j = N - 2; j >= 0; j--)
temp1[j] = Math.max(temp1[j + 1], mat[N - 1][j]);
let maxValue = Number.MIN_SAFE_INTEGER;
for (let i = N - 2; i >= 0; i--) {
temp2[N - 1] = Math.max(temp1[N - 1], mat[i][N - 1]);
for (let j = N - 2; j >= 0; j--) {
maxValue = Math.max(maxValue, temp1[j + 1] - mat[i][j]);
temp2[j] = Math.max(mat[i][j], temp1[j], temp2[j + 1]);
}
temp1.splice(0, N, ...temp2);
}
return maxValue;
}
const mat = [
[1, 2, -1, -4, -20],
[-8, -3, 4, 2, 1],
[3, 8, 6, 1, 3],
[-4, -1, 1, 7, -6],
[0, -4, 10, -5, 1]
];
console.log( "Maximum Value is" , findMaxValue(mat));
|
Output
Maximum Value is 18
The time complexity of the findMaxValue() function is O(N^2) because it iterates over each element of the matrix exactly once.
The space complexity of the function is O(N) because it uses two vectors of size N (temp1 and temp2) to store the maximum values seen so far in each row and column.
This article is contributed by Aarti_Rathi and Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Dec, 2023
Like Article
Save Article