Maximize product of a matrix by repeatedly multiplying pairs of adjacent cells with -1
Given a matrix mat[][] of dimensions NxN consisting of integers, the task is to find the maximum product of elements of the matrix mat[][] possible repeatedly multiplying pairs of adjacent cells by -1.
Note: The value of any matrix element can be changed by -1 at most once.
Examples:
Input: arr[][] = {{1, -2}, {2, -3}}
Output: 12
Explanation:
Multiply arr[0][1] and arr[1][1] by -1. Therefore, product of the matrix = = 1 * 2 * 2 * 3 = 12.
Input: arr[][] = {{2, 2}, {-3, 1}};
Output: 216
Approach: The given problem can be solved based on the following observations:
Follow the below steps to solve the problem:
- Find the number of negative numbers(say count) and 0s(say zeros) in the given matrix by traversing the matrix mat[][].
- If the count is even and zeros is 1, then print the product of all numbers in the matrix except that 0.
- Otherwise, find the minimum absolute element in the matrix, change it to 1 and then print the product of all numbers in the matrix as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProduct(vector<vector< int > > arr)
{
int N = arr.size();
int cnt = 0;
int zeros = 0;
int maxValue = INT_MIN;
vector< int > v;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (arr[i][j] < 0) {
maxValue = max(maxValue,
arr[i][j]);
cnt++;
}
if (arr[i][j] == 0)
zeros++;
}
}
if (zeros > 1) {
cout << "0" ;
}
int product = 1;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (arr[i][j] == 0)
continue ;
product *= (arr[i][j]);
}
}
if (cnt % 2 == 0) {
return product;
}
return product / maxValue;
}
int main()
{
vector<vector< int > > mat
= { { 2, -2 }, { -3, 1 } };
cout << maxProduct(mat);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int maxProduct( int [][] arr)
{
int N = arr.length;
int cnt = 0 ;
int zeros = 0 ;
int maxValue = Integer.MIN_VALUE;
int v[];
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++) {
if (arr[i][j] < 0 ) {
maxValue = Math.max(maxValue,
arr[i][j]);
cnt++;
}
if (arr[i][j] == 0 )
zeros++;
}
}
if (zeros > 1 ) {
System.out.println( "0" );
}
int product = 1 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++) {
if (arr[i][j] == 0 )
continue ;
product *= (arr[i][j]);
}
}
if (cnt % 2 == 0 ) {
return product;
}
return product / maxValue;
}
public static void main(String[] args)
{
int [][] mat
= { { 2 , - 2 }, { - 3 , 1 } };
System.out.println(maxProduct(mat));
}
}
|
Python3
def maxProduct(arr):
N = len (arr)
cnt = 0
zeros = 0
maxValue = - 10 * * 9
v = []
for i in range (N):
for j in range (N):
if (arr[i][j] < 0 ):
maxValue = max (maxValue, arr[i][j])
cnt + = 1
if (arr[i][j] = = 0 ):
zeros + = 1
if (zeros > 1 ):
print ( "0" )
product = 1
for i in range (N):
for j in range (N):
if (arr[i][j] = = 0 ):
continue
product * = (arr[i][j])
if (cnt % 2 = = 0 ):
return product
return product / / maxValue
if __name__ = = '__main__' :
mat = [ [ 2 , - 2 ], [ - 3 , 1 ] ]
print (maxProduct(mat))
|
C#
using System;
class GFG{
static int maxProduct( int [,] arr)
{
int N = arr.GetLength(0);
int cnt = 0;
int zeros = 0;
int maxValue = Int32.MinValue;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
if (arr[i, j] < 0)
{
maxValue = Math.Max(maxValue,
arr[i, j]);
cnt++;
}
if (arr[i, j] == 0)
zeros++;
}
}
if (zeros > 1)
{
Console.WriteLine( "0" );
}
int product = 1;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
if (arr[i, j] == 0)
continue ;
product *= (arr[i, j]);
}
}
if (cnt % 2 == 0)
{
return product;
}
return Math.Abs(product / maxValue);
}
public static void Main(String []args)
{
int [,] mat = { { 2, -2 }, { -3, 1 } };
Console.Write(maxProduct(mat));
}
}
|
Javascript
<script>
function maxProduct(arr)
{
let N = arr.length;
let cnt = 0;
let zeros = 0;
let maxValue = Number.MIN_VALUE;
let v = new Array();
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (arr[i][j] < 0) {
maxValue = Math.max(maxValue,
arr[i][j]);
cnt++;
}
if (arr[i][j] == 0)
zeros++;
}
}
if (zeros > 1) {
document.write( "0" );
}
let product = 1;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (arr[i][j] == 0)
continue ;
product *= (arr[i][j]);
}
}
if (cnt % 2 == 0) {
return product;
}
return product / maxValue;
}
let mat
= [[ 2, -2 ], [ -3, 1 ]];
document.write(maxProduct(mat));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
10 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...