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
function maximumAND ( arr , n , m ) { let tot = 1 << n ; let mx = 0 ; for ( let bm = 0 ; bm < tot ; bm ++ ) { let andans = 0 ; let count = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( ( ( bm >> i ) & 1 ) != 0 ) { if ( count == 0 ) { andans = arr [ i ] ; } else { andans = andans & arr [ i ] ; } count ++ ; } } if ( count == ( m + 1 ) ) { mx = Math . max ( mx , andans ) ; } } return mx ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; let M = 2 ; document . write ( maximumAND ( arr , N , M ) ) ;