text: Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array | Javascript Program to implement the above approach ; Function to maximize the value for the given function and the array elements ; Vector array to maintain which bit is set for which integer in the given array by saving index of that integer ; Check if j - th bit is set for i - th integer ; Push the index of that integer in setBit [ j ] ; Find the element having highest significant set bit unset in other elements ; Place that integer at 0 - th index ; Store the maximum AND value ; Return the answer ; Driver Code ; Function call
code: var size_int = 32 ; function functionMax ( arr , n ) { var setBit = Array . from ( Array ( 32 ) , ( ) => new Array ( ) ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < size_int ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) setBit [ j ] . push ( i ) ; } } for ( var i = size_int - 1 ; i >= 0 ; i -- ) { if ( setBit [ i ] . length == 1 ) { [ arr [ 0 ] , arr [ setBit [ i ] [ 0 ] ] ] = [ arr [ setBit [ i ] [ 0 ] ] , arr [ 0 ] ] ; break ; } } var maxAnd = arr [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) { maxAnd = maxAnd & ( ~ arr [ i ] ) ; } return maxAnd ; } var arr = [ 1 , 2 , 4 , 8 , 16 ] ; var n = arr . length ; document . write ( functionMax ( arr , n ) ) ;