Maximize sum of Bitwise AND of same | Function to calculate sum of Bitwise AND of same indexed elements of the arrays p [ ] and arr [ ] ; Stores the resultant sum ; Traverse the array ; Update sum of Bitwise AND ; Return the value obtained ; Function to generate all permutations and calculate the maximum sum of Bitwise AND of same indexed elements present in any permutation and an array arr [ ] ; If the size of the array is N ; Calculate cost of permutation ; Generate all permutations ; Update chosen [ i ] ; Update the permutation p [ ] ; Generate remaining permutations ; Return the resultant sum ; Function to find the maximum sum of Bitwise AND of same indexed elements in a permutation of first N natural numbers and arr [ ] ; Stores the resultant maximum sum ; Stores the generated permutation P ; Function call to store result ; Print the result ; Driven Program ; Function call
function calcScore ( p , arr , N ) { var ans = 0 ; for ( var i = 0 ; i < N ; i ++ ) { ans += ( p [ i ] & arr [ i ] ) ; } return ans ; } function getMaxUtil ( p , arr , ans , chosen , N ) { if ( p . length == N ) { ans = Math . max ( ans , calcScore ( p , arr , N ) ) ; return ans ; } for ( var i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) { continue ; } chosen [ i ] = true ; p . push ( i ) ; ans = getMaxUtil ( p , arr , ans , chosen , N ) ; chosen [ i ] = false ; p . pop ( ) ; } return ans ; } function getMax ( arr , N ) { var ans = 0 ; var chosen = Array ( N ) . fill ( false ) ; var p = [ ] ; var res = getMaxUtil ( p , arr , ans , chosen , N ) ; document . write ( res ) ; } var arr = [ 4 , 2 , 3 , 6 ] ; var N = arr . length ; getMax ( arr , N ) ;