Maximum increase in value of Matrix to keep maximum rows and columns unchanged
Last Updated :
07 Jun, 2021
Given a matrix mat[][] of dimensionM*N, the task is to find the total maximum possible value must be increased to each cell(say mat[i][j]) such that maximum element of row i and column j remains unchanged.
Examples:
Input: N = 3, M = 3, mat[][] = { {4, 1, 3}, {3, 1, 1}, {2, 2, 0} }
Output: 6
Explanation:
The given matrix is:
4 1 3
3 1 1
2 2 0
Row-wise maximum values are: {4, 3, 2}
Column-wise maximum values are: {4, 2, 3}
After increasing the elements without affecting the row-wise and column-wise maximum values:
4 2 3
3 2 3
2 2 2
The total increase in corresponding cells of the two matrix is ((0 + 1 + 0) + (0 + 1 + 2) + (0 + 0 + 2)) = 6.
Input: M = 4, N = 4, mat[][] = { {3, 0, 8, 4}, {2, 4, 5, 7}, {9, 2, 6, 3}, {0, 3, 1, 0} }
Output: 35
Explanation:
The given matrix is:
3 0 8 4
2 4 5 7
9 2 6 3
0 3 1 0
Row-wise maximum values are: {8, 7, 9, 3}
Column-wise maximum values are: {9, 4, 8, 7}
After increasing the elements without affecting the row-wise and column-wise maximum values:
8 4 8 7
7 4 7 7
9 4 8 7
3 3 3 3
The total increase in corresponding cells of the two matrix is ((5 + 4 + 0 + 3) + (5 + 0 + 2 + 0) + (0 + 2 + 2 + 4) + (3 + 0 + 2 + 3)) = 35.
Approach:
- Traverse along each row and column of the given matrix to store the maximum values for each row and column.
- Traverse the given matrix mat[][] and for each cell(say mat[i][j]) and add the difference between the current element and the minimum value of the maximum values along its corresponding row and column as:
difference = min(row[i], cols[j]) – mat[i][j]
- The total values added in the above operations is the required result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxIncrease(vector<vector< int > >& matrix)
{
int M = matrix.size();
int N = matrix[0].size();
vector< int > maxRowVal(M, 0);
vector< int > maxColVal(N, 0);
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
maxRowVal[i] = max(maxRowVal[i],
matrix[i][j]);
maxColVal[j] = max(maxColVal[j],
matrix[i][j]);
}
}
int totalIncrease = 0;
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
int needToIncrease
= min(maxRowVal[i],
maxColVal[j])
- matrix[i][j];
totalIncrease += needToIncrease;
}
}
return totalIncrease;
}
int main()
{
vector<vector< int > > matrix{ { 3, 0, 8, 4 },
{ 2, 4, 5, 7 },
{ 9, 2, 6, 3 },
{ 0, 3, 1, 0 } };
cout << maxIncrease(matrix)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxIncrease( int [][] matrix)
{
int M = matrix.length;
int N = matrix[ 0 ].length;
int []maxRowVal = new int [M];
int []maxColVal = new int [N];
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < N; j++)
{
maxRowVal[i] = Math.max(maxRowVal[i],
matrix[i][j]);
maxColVal[j] = Math.max(maxColVal[j],
matrix[i][j]);
}
}
int totalIncrease = 0 ;
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < N; j++)
{
int needToIncrease = Math.min(maxRowVal[i],
maxColVal[j]) -
matrix[i][j];
totalIncrease += needToIncrease;
}
}
return totalIncrease;
}
public static void main(String[] args)
{
int [][] matrix = { { 3 , 0 , 8 , 4 },
{ 2 , 4 , 5 , 7 },
{ 9 , 2 , 6 , 3 },
{ 0 , 3 , 1 , 0 } };
System.out.print(maxIncrease(matrix) + "\n" );
}
}
|
Python3
def maxIncrease(matrix):
M = len (matrix)
N = len (matrix[ 0 ])
maxRowVal = [ 0 ] * M
maxColVal = [ 0 ] * (N)
for i in range (M):
for j in range (N):
maxRowVal[i] = max (maxRowVal[i],
matrix[i][j])
maxColVal[j] = max (maxColVal[j],
matrix[i][j])
totalIncrease = 0
for i in range (M):
for j in range (N):
needToIncrease = ( min (maxRowVal[i],
maxColVal[j]) -
matrix[i][j])
totalIncrease + = needToIncrease
return totalIncrease
if __name__ = = '__main__' :
matrix = [ [ 3 , 0 , 8 , 4 ],
[ 2 , 4 , 5 , 7 ],
[ 9 , 2 , 6 , 3 ],
[ 0 , 3 , 1 , 0 ] ]
print (maxIncrease(matrix))
|
C#
using System;
class GFG{
static int maxIncrease( int [,] matrix)
{
int M = matrix.GetLength(0);
int N = matrix.GetLength(1);
int []maxRowVal = new int [M];
int []maxColVal = new int [N];
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
maxRowVal[i] = Math.Max(maxRowVal[i],
matrix[i, j]);
maxColVal[j] = Math.Max(maxColVal[j],
matrix[i, j]);
}
}
int totalIncrease = 0;
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
int needToIncrease = Math.Min(maxRowVal[i],
maxColVal[j]) -
matrix[i, j];
totalIncrease += needToIncrease;
}
}
return totalIncrease;
}
public static void Main(String[] args)
{
int [,] matrix = { { 3, 0, 8, 4 },
{ 2, 4, 5, 7 },
{ 9, 2, 6, 3 },
{ 0, 3, 1, 0 } };
Console.Write(maxIncrease(matrix) + "\n" );
}
}
|
Javascript
<script>
function maxIncrease(matrix)
{
var M = matrix.length;
var N = matrix[0].length;
var maxRowVal = Array(M).fill(0);
var maxColVal = Array(N).fill(0);
for ( var i = 0; i < M; i++) {
for ( var j = 0; j < N; j++) {
maxRowVal[i] = Math.max(maxRowVal[i],
matrix[i][j]);
maxColVal[j] = Math.max(maxColVal[j],
matrix[i][j]);
}
}
var totalIncrease = 0;
for ( var i = 0; i < M; i++) {
for ( var j = 0; j < N; j++) {
var needToIncrease
= Math.min(maxRowVal[i],
maxColVal[j])
- matrix[i][j];
totalIncrease += needToIncrease;
}
}
return totalIncrease;
}
var matrix = [ [ 3, 0, 8, 4 ],
[ 2, 4, 5, 7 ],
[ 9, 2, 6, 3 ],
[ 0, 3, 1, 0 ] ];
document.write( maxIncrease(matrix) + "<br>" );
</script>
|
Time Complexity: O(M*N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...