Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the final score ; Function to generate all the possible permutation and get the max score ; If arr [ ] length is equal to N process the permutation ; Generating the permutations ; If the current element is chosen ; Mark the current element as true ; Recursively call for next possible permutation ; Backtracking ; Return the ans ; Driver Code ; Stores the permutation ; To display the result
function calcScr ( arr ) { let ans = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) ans += ( i ^ arr [ i ] ) ; return ans ; } function getMax ( arr , ans , chosen , N ) { if ( arr . length == N ) { ans = Math . max ( ans , calcScr ( arr ) ) ; return ans ; } for ( let i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) continue ; chosen [ i ] = true ; arr . push ( i ) ; ans = getMax ( arr , ans , chosen , N ) ; chosen [ i ] = false ; arr . pop ( ) ; } return ans ; } let N = 2 ; let arr = [ ] ; let ans = - 1 ; let chosen = new Array ( N ) . fill ( false ) ; ans = getMax ( arr , ans , chosen , N ) ; document . write ( ans + " " ) ;