Maximize matrix sum by repeatedly multiplying pairs of adjacent elements with -1
Last Updated :
04 May, 2021
Given a matrix A[][] of dimensions M × N, the task is to find the maximum sum possible from a matrix by repeatedly selecting two adjacent matrix elements and multiplying both of their values with -1.
Examples:
Input: A[ ][ ] = {{4, -8, 6}, {3, 7, 2}}
Output: 26
Explanation:
Multiply mat[0][1] and mat[0][2] by -1. The matrix modifies to A[][] = {{4, 8, -6}, {3, 7, 2}}.
Multiply mat[0][2] and mat[1][2] by -1. The matrix modifies to A[][] = {{4, 8, 6}, {3, 7, -2}}.
Therefore, sum of the matrix = 26, which is the maximum sum possible.
Input: A[ ][ ] = {{2, 9, -5}, {6, 1, 3}, {-7, 4, 8}}
Output: 45
Explanation:
Multiply mat[0][2] and mat[1][2] by -1. The matrix modifies to A[][] = {{2, 9, 5}, {6, 1, -3}, {-7, 4, 8}}.
Multiply mat[1][1] and mat[1][2] by -1. The matrix modifies to A[][] = {{2, 9, 5}, {6, -1, 3}, {-7, 4, 8}}.
Multiply mat[1][1] and mat[2][1] by -1. The matrix modifies to A[][] = {{2, 9, 5}, {6, 1, 3}, {-7, -4, 8}}.
Multiply mat[2][0] and mat[2][1] by -1. The matrix modifies to A[][] = {{2, 9, 5}, {6, 1, 3}, {7, 4, 8}}.
Therefore, sum of the matrix = 45, which is the maximum sum possible.
Approach: To maximize the sum of the given matrix, perform the given operations such that the smallest element in a row or column is negative (if it is not possible to make all row and column elements positive). Follow the steps below to maximize the sum:
- Let the number of cells having negative values be x. If x is 0 i.e., there are no negative values, then the sum of the matrix is already maximum possible.
- Otherwise, take any negative value from the matrix and perform the given operations on that element and any of its adjacent cells. This results in the chosen element becoming positive.
- Now if the value of the chosen adjacent element is negative, then it will become positive or vice versa. This way, in each turn two negative values can be made positive. Now following two cases arise:
- If x is even: In this case, after x/2 turns, all the negative values can be made positive. Therefore, the maximum sum possible is equal to the sum of the absolute values of all cells.
- If x is odd: In this case, there will be one negative value left after performing operations in the manner described above. Now, to maximize the sum, the value getting subtracted or having a negative sign needs to be minimized. To do this, move the negative sign to the cell having minimum absolute value, say minVal. Therefore, the maximum sum possible is sum of the absolute values of all cells – 2*minVal.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getMaxSum(vector<vector< int > > A,
int M, int N)
{
int sum = 0;
int negative = 0;
int minVal = INT_MAX;
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
sum += abs (A[i][j]);
if (A[i][j] < 0) {
negative++;
}
minVal = min(minVal, abs (A[i][j]));
}
}
if (negative % 2) {
sum -= 2 * minVal;
}
cout << sum;
}
int main()
{
vector<vector< int > > A = {
{ 4, -8, 6 },
{ 3, 7, 2 }
};
int M = A.size();
int N = A[0].size();
getMaxSum(A, M, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void getMaxSum( int [][] A, int M, int N)
{
int sum = 0 ;
int negative = 0 ;
int minVal = Integer.MAX_VALUE;
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++) {
sum += Math.abs(A[i][j]);
if (A[i][j] < 0 ) {
negative++;
}
minVal
= Math.min(minVal, Math.abs(A[i][j]));
}
}
if (negative % 2 != 0 ) {
sum -= 2 * minVal;
}
System.out.println(sum);
}
public static void main(String[] args)
{
int [][] A = { { 4 , - 8 , 6 }, { 3 , 7 , 2 } };
int M = A.length;
int N = A[ 0 ].length;
getMaxSum(A, M, N);
}
}
|
Python3
import sys
def getMaxSum(A, M, N):
sum = 0
negative = 0
minVal = sys.maxsize
for i in range (M):
for j in range (N):
sum + = abs (A[i][j])
if (A[i][j] < 0 ):
negative + = 1
minVal = min (minVal, abs (A[i][j]))
if (negative % 2 ):
sum - = 2 * minVal
print ( sum )
if __name__ = = '__main__' :
A = [[ 4 , - 8 , 6 ], [ 3 , 7 , 2 ]]
M = len (A)
N = len (A[ 0 ])
getMaxSum(A, M, N)
|
C#
using System;
class GFG{
static void getMaxSum( int [,] A, int M, int N)
{
int sum = 0;
int negative = 0;
int minVal = Int32.MaxValue;
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
sum += Math.Abs(A[i, j]);
if (A[i, j] < 0)
{
negative++;
}
minVal = Math.Min(minVal,
Math.Abs(A[i, j]));
}
}
if (negative % 2 != 0)
{
sum -= 2 * minVal;
}
Console.Write(sum);
}
public static void Main()
{
int [, ] A = { { 4, -8, 6 },
{ 3, 7, 2 } };
int M = A.GetLength(0);
int N = A.GetLength(1);
getMaxSum(A, M, N);
}
}
|
Javascript
<script>
function getMaxSum(A,M,N)
{
let sum = 0;
let negative = 0;
let minVal = Number.MAX_VALUE;
for (let i = 0; i < M; i++) {
for (let j = 0; j < N; j++) {
sum += Math.abs(A[i][j]);
if (A[i][j] < 0) {
negative++;
}
minVal
= Math.min(minVal, Math.abs(A[i][j]));
}
}
if (negative % 2 != 0) {
sum -= 2 * minVal;
}
document.write(sum);
}
let A = [[4, -8, 6 ], [ 3, 7, 2 ]];
let M = A.length;
let N = A[0].length;
getMaxSum(A, M, N);
</script>
|
Time Complexity: O(M*N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...