Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | 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 ; Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the final score ; Driver Code ; Stores the permutation ; To display the result
def getMax ( arr , ans , chosen , N ) : NEW_LINE INDENT if len ( arr ) == N : NEW_LINE INDENT ans = max ( ans , calcScr ( arr ) ) NEW_LINE return ans NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if chosen [ i ] : NEW_LINE INDENT continue NEW_LINE DEDENT chosen [ i ] = True NEW_LINE arr . append ( i ) NEW_LINE ans = getMax ( arr , ans , chosen , N ) NEW_LINE chosen [ i ] = False NEW_LINE arr . pop ( ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def calcScr ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT ans += ( i ^ arr [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 2 NEW_LINE arr = [ ] NEW_LINE ans = - 1 NEW_LINE chosen = [ False for i in range ( N ) ] NEW_LINE ans = getMax ( arr , ans , chosen , N ) NEW_LINE print ( ans ) NEW_LINE