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++
// CPP to maximize matrix result
#include<bits/stdc++.h>
using namespace std;
#define n 4
// utility function for maximize matrix result
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];
// diagonals below longest diagonal
// starting from last element of matrix
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;
}
}
}
// diagonals above longest diagonal
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;
}
}
}
// calculating result
for (int i=0; i<n; i++)
{
for (int j=0; j<n;j++)
res += (i+j+2) * A[i][j];
}
return res;
}
// driver program
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
// Java to maximize matrix result
class GFG {
static final int n = 4;
// utility function for maximize matrix result
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];
}
}
// diagonals below longest diagonal
// starting from last element of matrix
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;
}
}
}
// diagonals above longest diagonal
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;
}
}
}
// calculating result
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
res += (i + j + 2) * A[i][j];
}
}
return res;
}
// driver program
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));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python to maximize matrix result
n = 4
# utility function for maximize
# matrix result
def maxMatrix(A, M):
sum, res = 0, 0
for i in range(n):
for j in range(n):
sum += A[i][j]
# diagonals below longest diagonal
# starting from last element of matrix
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
# diagonals above longest diagonal
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
# calculating result
for i in range(n):
for j in range(n):
res += (i + j + 2) * A[i][j]
return res
# Driver Code
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))
# This code is contributed by 29AjayKumar
C#
// C# to maximize matrix result
using System;
public class GFG {
static readonly int n = 4;
// utility function for maximize matrix result
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];
}
}
// diagonals below longest diagonal
// starting from last element of matrix
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;
}
}
}
// diagonals above longest diagonal
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;
}
}
}
// calculating result
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
res += (i + j + 2) * A[i,j];
}
}
return res;
}
// driver program
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));
}
}
// This code is contributed by Rajput-Ji
PHP
<?php
// PHP to maximize matrix result
$n = 4;
// function for maximize
// matrix result
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];
// diagonals below longest diagonal
// starting from last element of matrix
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;
}
}
}
// diagonals above longest diagonal
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;
}
}
}
// calculating result
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
$res += ($i + $j + 2) *
$A[$i][$j];
}
return $res;
}
// Driver Code
$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);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript to maximize matrix result
let n = 4;
// utility function for maximize matrix result
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];
}
}
// diagonals below longest diagonal
// starting from last element of matrix
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;
}
}
}
// diagonals above longest diagonal
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;
}
}
}
// calculating result
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));
// This code is contributed by divyesh072019.
</script>
Time Complexity: O(n2)
Auxiliary Space: O(1)
Similar Reads
Find X such that the given expression is maximized Given two integers A and B, the task is to output the maximum value of (X % B) * ((A â X) % B) by finding the appropriate value of X such that (0 <= X <= A). Note: If there are multiple values of X, output any valid value. Examples: Input: A = 4, B = 7Output: 2Explanation: As we know choose th
5 min read
Find the Maximum possible Sum for the given conditions Given an array arr[] of size N, the task is to find the maximum possible sum of the array by following the given conditions: At every step, only one element can be used to increase the sum.If some element K is selected from the array, the remaining numbers in the array get reduced by one.The element
6 min read
Maximize the value of the given expression Given three non-zero integers a, b and c. The task is to find the maximum value possible by putting addition and multiplication signs between them in any order. Note: Rearrangement of integers is allowed but addition and multiplication sign must be used once. Braces can also be placed between equati
8 min read
Maximum candies two friends can eat Given six integers X, Y, cnt1, cnt2, C1, and C2, where: X and Y represent the maximum calories two friends can consume,cnt1 and cnt2 are the numbers of two types of candies,C1 and C2 are the calories present in each candy of the two types of available candies. The task is to find the maximum number
7 min read
Maximize profit in buying and selling stocks with Rest condition The price of a stock on each day is given in an array arr[] for N days, the task is to find the maximum profit that can be made by buying and selling the stocks in those days with conditions that the stock must be sold before buying again and stock cannot be bought on the next day of selling a stock
15+ min read