📌  相关文章
📜  通过重复将相邻单元对与 -1 相乘来最大化矩阵的乘积

📅  最后修改于: 2021-09-08 12:38:21             🧑  作者: Mango

给定矩阵mat[][] 维度NxN由整数组成,任务是找到矩阵mat[][]元素的最大乘积,可能重复将相邻单元对乘以-1
注意:任何矩阵元素的值最多可以改变-1一次。

例子:

方法:根据以下观察可以解决给定的问题:

  • 如果矩阵中存在的负数计数为偶数,并且矩阵中0的计数为1 ,则将该0更改为1 ,然后打印矩阵中所有元素的乘积作为结果。否则,打印0作为结果。
  • 否则,将最小绝对值更改为1 ,然后打印矩阵中所有元素的乘积作为结果。

请按照以下步骤解决问题:

  • 通过遍历矩阵mat[][]在给定矩阵中找到负数(比如count )和0 (比如zeros )的数量。
  • 如果计数为偶数且1 ,则打印矩阵中除0之外的所有数字的乘积。
  • 否则,找到矩阵中的最小绝对元素,将其更改为1 ,然后打印矩阵中所有数字的乘积作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate maximum
// product of a matrix by multiplying
// pair of adjacent cells with -1
int maxProduct(vector > arr)
{
    int N = arr.size();
 
    // Stores the count of negative
    // numbers in the given matrix
    int cnt = 0;
 
    // Stores the count of 0s in
    // the given matrix arr[][]
    int zeros = 0;
 
    // Stores the minimum absolute value
    int maxValue = INT_MIN;
 
    vector v;
 
    // Traverse the given matrix
    for (int i = 0; i < N; i++) {
 
        for (int j = 0; j < N; j++) {
 
            // Count negative numbers
            if (arr[i][j] < 0) {
 
                // Update the maximum
                // negative value
                maxValue = max(maxValue,
                               arr[i][j]);
                cnt++;
            }
 
            // Increment the count
            // of 0s if exists
            if (arr[i][j] == 0)
                zeros++;
        }
    }
 
    // If there are more than one
    // 0, then print product as 0
    if (zeros > 1) {
        cout << "0";
    }
 
    int product = 1;
 
    // Otherwise, find the product of all
    // the elements of the matrix arr[][]
    for (int i = 0; i < N; i++) {
 
        for (int j = 0; j < N; j++) {
 
            if (arr[i][j] == 0)
                continue;
 
            // Update the product
            product *= (arr[i][j]);
        }
    }
 
    // If count of negative
    // elements is even
    if (cnt % 2 == 0) {
        return product;
    }
 
    return product / maxValue;
}
// Driver Code
int main()
{
    vector > mat
        = { { 2, -2 }, { -3, 1 } };
    cout << maxProduct(mat);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG
{
 
// Function to calculate maximum
// product of a matrix by multiplying
// pair of adjacent cells with -1
static int maxProduct(int[][] arr)
{
    int N = arr.length;
  
    // Stores the count of negative
    // numbers in the given matrix
    int cnt = 0;
  
    // Stores the count of 0s in
    // the given matrix arr[][]
    int zeros = 0;
  
    // Stores the minimum absolute value
    int maxValue = Integer.MIN_VALUE;
  
    int v[];
  
    // Traverse the given matrix
    for (int i = 0; i < N; i++) {
  
        for (int j = 0; j < N; j++) {
  
            // Count negative numbers
            if (arr[i][j] < 0) {
  
                // Update the maximum
                // negative value
                maxValue = Math.max(maxValue,
                               arr[i][j]);
                cnt++;
            }
  
            // Increment the count
            // of 0s if exists
            if (arr[i][j] == 0)
                zeros++;
        }
    }
  
    // If there are more than one
    // 0, then print product as 0
    if (zeros > 1) {
         System.out.println("0");
    }
  
    int product = 1;
  
    // Otherwise, find the product of all
    // the elements of the matrix arr[][]
    for (int i = 0; i < N; i++) {
  
        for (int j = 0; j < N; j++) {
  
            if (arr[i][j] == 0)
                continue;
  
            // Update the product
            product *= (arr[i][j]);
        }
    }
  
    // If count of negative
    // elements is even
    if (cnt % 2 == 0) {
        return product;
    }
  
    return product / maxValue;
}
 
    // Driver Code
    public static void main(String[] args)
    {
 
         int[][] mat
        = { { 2, -2 }, { -3, 1 } };
    System.out.println(maxProduct(mat));
    }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python3 program for the above approach
 
# Function to calculate maximum
# product of a matrix by multiplying
# pair of adjacent cells with -1
def maxProduct(arr):
 
    N = len(arr)
 
    # Stores the count of negative
    # numbers in the given matrix
    cnt = 0
 
    # Stores the count of 0s in
    # the given matrix arr[][]
    zeros = 0
 
    # Stores the minimum absolute value
    maxValue = -10**9
 
    v = []
 
    # Traverse the given matrix
    for i in range(N):
        for j in range(N):
 
            # Count negative numbers
            if (arr[i][j] < 0):
 
                # Update the maximum
                # negative value
                maxValue = max(maxValue, arr[i][j])
                cnt += 1
 
            # Increment the count
            # of 0s if exists
            if (arr[i][j] == 0):
                zeros += 1
 
    # If there are more than one
    # 0, then prproduct as 0
    if (zeros > 1):
        print("0")
 
    product = 1
 
    # Otherwise, find the product of all
    # the elements of the matrix arr[][]
    for i in range(N):
        for j in range(N):
            if (arr[i][j] == 0):
                continue
 
            # Update the product
            product *= (arr[i][j])
 
    # If count of negative
    # elements is even
    if (cnt % 2 == 0):
        return product
 
    return product // maxValue
 
# Driver Code
if __name__ == '__main__':
 
    mat = [ [ 2, -2 ], [ -3, 1 ] ]
    print (maxProduct(mat))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to calculate maximum
// product of a matrix by multiplying
// pair of adjacent cells with -1
static int maxProduct(int[,] arr)
{
    int N = arr.GetLength(0);
  
    // Stores the count of negative
    // numbers in the given matrix
    int cnt = 0;
  
    // Stores the count of 0s in
    // the given matrix arr[][]
    int zeros = 0;
  
    // Stores the minimum absolute value
    int maxValue = Int32.MinValue;
  
    //int[] v;
  
    // Traverse the given matrix
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // Count negative numbers
            if (arr[i, j] < 0)
            {
                 
                // Update the maximum
                // negative value
                maxValue = Math.Max(maxValue,
                                    arr[i, j]);
                cnt++;
            }
  
            // Increment the count
            // of 0s if exists
            if (arr[i, j] == 0)
                zeros++;
        }
    }
  
    // If there are more than one
    // 0, then print product as 0
    if (zeros > 1)
    {
         Console.WriteLine("0");
    }
     
    int product = 1;
  
    // Otherwise, find the product of all
    // the elements of the matrix arr[][]
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            if (arr[i, j] == 0)
                continue;
  
            // Update the product
            product *= (arr[i, j]);
        }
    }
  
    // If count of negative
    // elements is even
    if (cnt % 2 == 0)
    {
        return product;
    }
    return Math.Abs(product / maxValue);
}
 
// Driver code
public static void Main(String []args)
{
    int[,] mat = { { 2, -2 }, { -3, 1 } };
     Console.Write(maxProduct(mat));
}
}
 
// This code is contriobuted by code_hunt


Javascript


输出:
12

时间复杂度: O(N 2 )
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live