Find maximum xor of k elements in an array | C ++ implementation of the approach ; Function to return the maximum xor for a subset of size j from the given array ; If the subset is complete then return the xor value of the selected elements ; Return if already calculated for some mask and j at the i 'th index ; Initialize answer to 0 ; If we can still include elements in our subset include the i 'th element ; Exclude the i 'th element  ans store the max of both operations ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10000 NEW_LINE #define MAX_ELEMENT  50 NEW_LINE int dp [ MAX_ELEMENT ] [ MAX_ELEMENT ] [ MAX ] ; int Max_Xor ( int arr [ ] , int i , int j , int mask , int n ) { if ( i >= n ) { if ( j == 0 ) return mask ; else return 0 ; } if ( dp [ i ] [ j ] [ mask ] != -1 ) return dp [ i ] [ j ] [ mask ] ; int ans = 0 ; if ( j > 0 ) ans = Max_Xor ( arr , i + 1 , j - 1 , mask ^ arr [ i ] , n ) ; ans = max ( ans , Max_Xor ( arr , i + 1 , j , mask , n ) ) ; return dp [ i ] [ j ] [ mask ] = ans ; } int main ( ) { int arr [ ] = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 3 ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << Max_Xor ( arr , 0 , k , 0 , n ) ; return 0 ; }