Maximize matrix as per given condition
Last Updated :
15 Sep, 2023
you are given an N*N matrix, where each element of the matrix lies in the range 0 to M. You can apply the below operation on the matrix any number of times:
- Choose any two consecutive elements
- Increment one of them by 1 and decrease the other by 1
Note: The elements should remain within the range of 0 to M after applying the above operations.
The task is to find the maximum value of the expression shown below that can be obtained after performing the above operation on the matrix if required:
res += (i+j)*A[i][j]
for 0 <= i, j <= N
Examples:
Input : A[][] = {1, 2,
5, 1}
M = 5
Output : RESULT = 27
Matrix : 0 0
4 5
Input : A[][] = {3, 4,
5, 4}
M = 6
Output : RESULT = 43
Matrix : 0 4
6 6
Algorithm :
Below is the step-by-step algorithm to do this:
- First of all, calculate the sum of all elements of the given matrix as SUM.
- Start from last element that i.e. A(n, n) and move backward towards A(0,0) anti-diagonally as A(n, n), A(n, n-1), A(n-1, n), A(n, n-2), A(n-1, n-1), A(n-2, n)…..
- Fill up each cell of a matrix with M and update SUM = SUM- M for each element till SUM < M. Now, Fill the SUM value at the next place in order if it is greater than zero and all other remaining places as zero.
- Finally, you can calculate RESULT as per the above-mentioned formula.
Example :
Input Matrix:
Solution Matrix after applying the above algorithm :
Below is the implementation of the above idea :
C++
#include<bits/stdc++.h>
using namespace std;
#define n 4
int maxMatrix( int A[][n], int M)
{
int sum = 0, res = 0;
for ( int i=0; i<n ; i++)
for ( int j=0; j<n; j++)
sum += A[i][j];
for ( int j=n-1; j>0; j--)
{
for ( int i=0; i<n-j; i++)
{
if (sum > M)
{
A[n-1-i][j+i] = M;
sum -= M;
}
else
{
A[n-1-i][j+i] = sum;
sum -= sum;
}
}
}
for ( int i=n-1; i>=0; i--)
{
for ( int j=0; j<=i; j++)
{
if (sum > M)
{
A[i-j][j] = M;
sum -= M;
}
else
{
A[i-j][j] = sum;
sum -= sum;
}
}
}
for ( int i=0; i<n; i++)
{
for ( int j=0; j<n;j++)
res += (i+j+2) * A[i][j];
}
return res;
}
int main()
{
int A[n][n] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 1, 1, 2,
3, 4, 5, 6};
int m = 9;
cout << maxMatrix(A, m);
return 0;
}
|
Java
class GFG {
static final int n = 4 ;
static int maxMatrix( int A[][], int M) {
int sum = 0 , res = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
sum += A[i][j];
}
}
for ( int j = n - 1 ; j > 0 ; j--) {
for ( int i = 0 ; i < n - j; i++) {
if (sum > M) {
A[n - 1 - i][j + i] = M;
sum -= M;
} else {
A[n - 1 - i][j + i] = sum;
sum -= sum;
}
}
}
for ( int i = n - 1 ; i >= 0 ; i--) {
for ( int j = 0 ; j <= i; j++) {
if (sum > M) {
A[i - j][j] = M;
sum -= M;
} else {
A[i - j][j] = sum;
sum -= sum;
}
}
}
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
res += (i + j + 2 ) * A[i][j];
}
}
return res;
}
static public void main(String[] args) {
int A[][] = {{ 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 1 , 1 , 2 },
{ 3 , 4 , 5 , 6 }};
int m = 9 ;
System.out.println(maxMatrix(A, m));
}
}
|
Python3
n = 4
def maxMatrix(A, M):
sum , res = 0 , 0
for i in range (n):
for j in range (n):
sum + = A[i][j]
for j in range (n - 1 , 0 , - 1 ):
for i in range (n - j):
if ( sum > M):
A[n - 1 - i][j + i] = M
sum - = M
else :
A[n - 1 - i][j + i] = sum
sum - = sum
for i in range (n - 1 , - 1 , - 1 ):
for j in range (i + 1 ):
if ( sum > M):
A[i - j][j] = M
sum - = M
else :
A[i - j][j] = sum
sum - = sum
for i in range (n):
for j in range (n):
res + = (i + j + 2 ) * A[i][j]
return res
if __name__ = = '__main__' :
A = [[ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 1 , 1 , 2 ],
[ 3 , 4 , 5 , 6 ]]
m = 9
print (maxMatrix(A, m))
|
C#
using System;
public class GFG {
static readonly int n = 4;
static int maxMatrix( int [,]A, int M) {
int sum = 0, res = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
sum += A[i,j];
}
}
for ( int j = n - 1; j > 0; j--) {
for ( int i = 0; i < n - j; i++) {
if (sum > M) {
A[n - 1 - i,j + i] = M;
sum -= M;
} else {
A[n - 1 - i,j + i] = sum;
sum -= sum;
}
}
}
for ( int i = n - 1; i >= 0; i--) {
for ( int j = 0; j <= i; j++) {
if (sum > M) {
A[i - j,j] = M;
sum -= M;
} else {
A[i - j,j] = sum;
sum -= sum;
}
}
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
res += (i + j + 2) * A[i,j];
}
}
return res;
}
static public void Main() {
int [,]A= {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 1, 1, 2},
{3, 4, 5, 6}};
int m = 9;
Console.Write(maxMatrix(A, m));
}
}
|
PHP
<?php
$n = 4;
function maxMatrix( $A , $M )
{
global $n ;
$sum = 0; $res = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $n ; $j ++)
$sum += $A [ $i ][ $j ];
for ( $j = $n - 1; $j > 0; $j --)
{
for ( $i = 0; $i < $n - $j ; $i ++)
{
if ( $sum > $M )
{
$A [ $n - 1 - $i ][ $j + $i ] = $M ;
$sum -= $M ;
}
else
{
$A [ $n - 1 - $i ][ $j + i] = $sum ;
$sum -= $sum ;
}
}
}
for ( $i = $n - 1; $i >= 0; $i --)
{
for ( $j = 0; $j <= $i ; $j ++)
{
if ( $sum > $M )
{
$A [ $i - $j ][ $j ] = $M ;
$sum -= $M ;
}
else
{
$A [ $i - $j ][ $j ] = $sum ;
$sum -= $sum ;
}
}
}
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
$res += ( $i + $j + 2) *
$A [ $i ][ $j ];
}
return $res ;
}
$A = array ( array (1, 2, 3, 4),
array (5, 6, 7, 8),
array (9, 1, 1, 2),
array (3, 4, 5, 6));
$m = 9;
echo maxMatrix( $A , $m );
?>
|
Javascript
<script>
let n = 4;
function maxMatrix(A, M) {
let sum = 0, res = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
sum += A[i][j];
}
}
for (let j = n - 1; j > 0; j--) {
for (let i = 0; i < n - j; i++) {
if (sum > M) {
A[n - 1 - i][j + i] = M;
sum -= M;
} else {
A[n - 1 - i][j + i] = sum;
sum -= sum;
}
}
}
for (let i = n - 1; i >= 0; i--) {
for (let j = 0; j <= i; j++) {
if (sum > M) {
A[i - j][j] = M;
sum -= M;
} else {
A[i - j][j] = sum;
sum -= sum;
}
}
}
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
res += (i + j + 2) * A[i][j];
}
}
return res;
}
let A = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 1, 1, 2],
[3, 4, 5, 6]];
let m = 9;
document.write(maxMatrix(A, m));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...