📌  相关文章
📜  最小化在给定矩阵中所有对均具有按位与的数组的总和

📅  最后修改于: 2021-05-17 05:43:55             🧑  作者: Mango

给定一个正方形对称矩阵垫[] [大小为N]时,任务是找到大小为N的一个阵列ARR []的最小总和可能的,使得对于i!= j的按位AND ARR的值[I ]arr [j]mat [i] [j]

例子:

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

  • 两个数字的按位与的二进制表示形式仅具有在两个数字中均设置的那些位。
  • 因此,从按位的属性,并将其可以观察到,所得的阵列中的数必须具有所有在矩阵元素中的至少一个设置的第i行中的位置位。
  • 因此,在i中的数所得到的阵列中的位置是按位矩阵的所有元素,或在第i行。

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

  • 将变量say sum初始化为0 ,以存储数组的最小可能和。
  • 迭代范围[0,N – 1],并在第i行按位加的或全部的矩阵的元素的值,除了垫[I] [I]到变量sum。
  • 完成上述步骤后,将总和的值打印为可能的总和

下面是上述方法的实现:

C++
// C++ program of the above approach
 
#include 
using namespace std;
 
// Function to find the minimum sum
// of the array such that Bitwise
// AND of arr[i] ana arr[j] is mat[i][j]
int findMinSum(vector > mat,
               int N)
{
    // Stores the minimum possible sum
    int sum = 0;
 
    // Traverse the range [0, N - 1]
    for (int i = 0; i < N; i++) {
 
        // Stores the Bitwise OR of
        // all the element of a row
        int res = 0;
 
        // Traverse the range [0, N - 1]
        for (int j = 0; j < N; j++) {
 
            // If i not equal to j
            if (i != j) {
 
                // Update the value of
                // res
                res |= mat[i][j];
            }
        }
 
        // Increment the sum by res
        sum += res;
    }
 
    // Return minimum possible sum
    return sum;
}
 
// Driver Code
int main()
{
    vector > mat
        = { { -1, 2, 3 }, { 9, -1, 7 }, { 4, 5, -1 } };
    int N = mat.size();
    cout << findMinSum(mat, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the minimum sum
// of the array such that Bitwise
// AND of arr[i] ana arr[j] is mat[i][j]
static int findMinSum(int mat[][], int N)
{
     
    // Stores the minimum possible sum
    int sum = 0;
 
    // Traverse the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Stores the Bitwise OR of
        // all the element of a row
        int res = 0;
 
        // Traverse the range [0, N - 1]
        for(int j = 0; j < N; j++)
        {
             
            // If i not equal to j
            if (i != j)
            {
                 
                // Update the value of
                // res
                res |= mat[i][j];
            }
        }
 
        // Increment the sum by res
        sum += res;
    }
 
    // Return minimum possible sum
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
    int mat[][] = { { -1, 2, 3 },
                    { 9, -1, 7 },
                    { 4, 5, -1 } };
    int N = mat.length;
 
    System.out.println(findMinSum(mat, N));
}
}
 
// This code is contributed by Kingash


Python3
# Python 3 program of the above approach
 
# Function to find the minimum sum
# of the array such that Bitwise
# AND of arr[i] ana arr[j] is mat[i][j]
def findMinSum(mat, N):
   
    # Stores the minimum possible sum
    sum1 = 0
 
    # Traverse the range [0, N - 1]
    for i in range(N):
       
        # Stores the Bitwise OR of
        # all the element of a row
        res = 0
 
        # Traverse the range [0, N - 1]
        for j in range(N):
           
            # If i not equal to j
            if (i != j):
               
                # Update the value of
                # res
                res |= mat[i][j]
 
        # Increment the sum by res
        sum1 += res
 
    # Return minimum possible sum
    return sum1
 
  # Driver code
if __name__ == '__main__':
    mat = [[-1, 2, 3], [9, -1, 7], [4, 5,-1]]
    N = 3
    print(findMinSum(mat, N))
 
    # This code is contributed by ipg2016107.


Javascript


输出:
23

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