Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum | Function to find the maximum Bitwise AND of connected components possible by connecting a graph using M edges ; Stores total number of ways to connect the graph ; Stores the maximum Bitwise AND ; Iterate over the range [ 0 , 2 ^ n ] ; Store the Bitwise AND of the connected vertices ; Store the count of the connected vertices ; Check for all the bits ; If i - th bit is set ; If the first vertex is added ; Set andans equal to arr [ i ] ; Calculate Bitwise AND of arr [ i ] with andans ; Increase the count of connected vertices ; If number of connected vertices is ( m + 1 ) , no cycle is formed ; Find the maximum Bitwise AND value possible ; Return the maximum Bitwise AND possible ; Driver Code
def maximumAND ( arr , n , m ) : NEW_LINE INDENT tot = 1 << n NEW_LINE mx = 0 NEW_LINE for bm in range ( tot ) : NEW_LINE INDENT andans = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( bm >> i ) & 1 ) : NEW_LINE INDENT if ( count == 0 ) : NEW_LINE INDENT andans = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT andans = andans & arr [ i ] NEW_LINE DEDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == ( m + 1 ) ) : NEW_LINE INDENT mx = max ( mx , andans ) NEW_LINE DEDENT DEDENT return mx NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE M = 2 NEW_LINE print ( maximumAND ( arr , N , M ) ) NEW_LINE DEDENT