Maximize matrix sum by flipping the sign of any adjacent pairs
Given a matrix mat[] of dimension N*N, the task is to find the maximum sum of matrix elements by flipping the signs of the adjacent element any number of times.
Examples:
Input: mat[][] = [[2, -2], [-2, 2]]
OutputL: 8
Explanation:
Follow the steps below to find the maximum sum of matrix as:
- Flipping the sign of adjacent element (arr[0][0], arr[0][1]) modifies the matrix to mat[][] = {{-2, 2}, {-2, 2}}.
- Flipping the sign of adjacent element (arr[0][0], arr[1][0]) modifies the matrix to mat[][] = {{2, 2}, {2, 2}}.
Now, the sum of matrix elements is 8, which is the maximum among all possible flipping of adjacent matrix elements.
Input: mat[][] = [[1, 2, 3], [-1, -2, -3], [1, 2, 3]]
Output: 16
Approach: The given problem can be solved by observing the fact that if there are an even number of negatives in the matrix, then all those elements can be converted to positive elements to get the maximum sum. Otherwise, all matrix elements can be flipped except the one negative elements. Follow the steps below to solve the problem:
- Find the sum of absolute values of all matrix elements and store it in a variable say S.
- Find the matrix element with minimum absolute values and store it in a variable say minElement.
- If the count of negative elements in the given matrix mat[][] is even, then print the maximum sum as S. Otherwise, print the value of (S – 2*minElement) as the resultant sum by excluding the minimum element in the sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum(vector<vector< int > >& matrix)
{
int r = matrix.size();
int c = matrix[0].size();
int sum = 0;
int mini = INT_MAX;
int count = 0;
for ( int i = 0; i < r; i++) {
for ( int j = 0; j < c; j++) {
int k = matrix[i][j];
mini = min(mini, abs (k));
if (k < 0)
count++;
sum += abs (k);
}
}
if (count % 2 == 0) {
return sum;
}
else {
return (sum - 2 * mini);
}
}
int main()
{
vector<vector< int > > matrix
= { { 2, -2 }, { -2, 2 } };
cout << maxSum(matrix);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int maxSum( int [][] matrix)
{
int r = matrix.length;
int c = matrix[ 0 ].length;
int sum = 0 ;
int mini = Integer.MAX_VALUE;
int count = 0 ;
for ( int i = 0 ; i < r; i++) {
for ( int j = 0 ; j < c; j++) {
int k = matrix[i][j];
mini = Math.min(mini, Math.abs(k));
if (k < 0 )
count++;
sum += Math.abs(k);
}
}
if (count % 2 == 0 ) {
return sum;
}
else {
return (sum - 2 * mini);
}
}
public static void main(String[] args)
{
int [][] matrix = { { 2 , - 2 }, { - 2 , 2 } };
System.out.print(maxSum(matrix));
}
}
|
Python3
import sys
def maxSum(matrix):
r = len (matrix)
c = len (matrix[ 0 ])
sum = 0
mini = sys.maxsize
count = 0
for i in range (r):
for j in range (c):
k = matrix[i][j]
mini = min (mini, abs (k))
if (k < 0 ):
count + = 1
sum + = abs (k)
if (count % 2 = = 0 ):
return sum
else :
return ( sum - 2 * mini)
if __name__ = = '__main__' :
matrix = [[ 2 , - 2 ],[ - 2 , 2 ]]
print (maxSum(matrix))
|
C#
using System;
public class GFG
{
static int maxSum( int [,] matrix)
{
int r = matrix.GetLength(0);
int c = matrix.GetLength(1);
int sum = 0;
int mini = int .MaxValue;
int count = 0;
for ( int i = 0; i < r; i++) {
for ( int j = 0; j < c; j++) {
int k = matrix[i,j];
mini = Math.Min(mini, Math.Abs(k));
if (k < 0)
count++;
sum += Math.Abs(k);
}
}
if (count % 2 == 0) {
return sum;
}
else {
return (sum - 2 * mini);
}
}
public static void Main( string [] args)
{
int [,] matrix = { { 2, -2 }, { -2, 2 } };
Console.Write(maxSum(matrix));
}
}
|
Javascript
<script>
function maxSum(matrix) {
let r = matrix.length;
let c = matrix[0].length;
let sum = 0;
let mini = Number.MAX_VALUE;
let count = 0;
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j++) {
let k = matrix[i][j];
mini = Math.min(mini, Math.abs(k));
if (k < 0)
count++;
sum += Math.abs(k);
}
}
if (count % 2 == 0) {
return sum;
}
else {
return (sum - 2 * mini);
}
}
let matrix
= [[2, -2], [-2, 2]];
document.write(maxSum(matrix));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
16 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...