Minimize sum of an array having Bitwise AND of all its pairs present in a given matrix | C ++ 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 ] ; Stores the minimum possible sum ; Traverse the range [ 0 , N - 1 ] ; Stores the Bitwise OR of all the element of a row ; Traverse the range [ 0 , N - 1 ] ; If i not equal to j ; Update the value of res ; Increment the sum by res ; Return minimum possible sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinSum ( vector < vector < int > > mat , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int res = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( i != j ) { res |= mat [ i ] [ j ] ; } } sum += res ; } return sum ; } int main ( ) { vector < vector < int > > mat = { { -1 , 2 , 3 } , { 9 , -1 , 7 } , { 4 , 5 , -1 } } ; int N = mat . size ( ) ; cout << findMinSum ( mat , N ) ; return 0 ; }