Minimize sum of an array having Bitwise AND of all its pairs present in a given matrix | 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
def findMinSum ( mat , N ) : NEW_LINE INDENT sum1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT res = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT res |= mat [ i ] [ j ] NEW_LINE DEDENT DEDENT sum1 += res NEW_LINE DEDENT return sum1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ - 1 , 2 , 3 ] , [ 9 , - 1 , 7 ] , [ 4 , 5 , - 1 ] ] NEW_LINE N = 3 NEW_LINE print ( findMinSum ( mat , N ) ) NEW_LINE DEDENT