Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | C # program for the above approach ; Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the readonly 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
using System ; using System . Collections . Generic ; public class GFG { static int calcScr ( List < int > arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . Count ; i ++ ) ans += ( i ^ arr [ i ] ) ; return ans ; } static int getMax ( List < int > arr , int ans , List < Boolean > chosen , int N ) { if ( arr . Count == N ) { ans = Math . Max ( ans , calcScr ( arr ) ) ; return ans ; } for ( int i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) continue ; chosen [ i ] = true ; arr . Add ( i ) ; ans = getMax ( arr , ans , chosen , N ) ; chosen [ i ] = false ; arr . Remove ( arr . Count - 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 2 ; List < int > arr = new List < int > ( ) ; int ans = - 1 ; List < bool > chosen = new List < bool > ( N ) ; for ( int i = 0 ; i < N ; i ++ ) chosen . Add ( false ) ; ans = getMax ( arr , ans , chosen , N ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }