Find maximum xor of k elements in an array
Given an array arr[] of N integers and an integer K. The task is to find the maximum xor subset of size K of the given array.
Examples:
Input: arr[] = {2, 5, 4, 1, 3, 7, 6, 8}, K = 3
Output: 15
We obtain 15 by selecting 4, 5, 6, 8Input: arr[] = {3, 4, 7, 7, 9}, K = 3
Output: 14
Naive approach: Iterate over all subsets of size K of the array and find maximum xor among them.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum xor for a // subset of size k from the given array int Max_Xor(int arr[], int n, int k) { // Initialize result int maxXor = INT_MIN; // Traverse all subsets of the array for (int i = 0; i < (1 << n); i++) { // __builtin_popcount() returns the number // of sets bits in an integer if (__builtin_popcount(i) == k) { // Initialize current xor as 0 int cur_xor = 0; for (int j = 0; j < n; j++) { // If jth bit is set in i then include // jth element in the current xor if (i & (1 << j)) cur_xor = cur_xor ^ arr[j]; } // Update maximum xor so far maxXor = max(maxXor, cur_xor); } } return maxXor; } // Driver code int main() { int arr[] = { 2, 5, 4, 1, 3, 7, 6, 8 }; int n = sizeof(arr) / sizeof(int); int k = 3; cout << Max_Xor(arr, n, k); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the maximum xor for a // subset of size k from the given array static int Max_Xor(int arr[], int n, int k) { // Initialize result int maxXor = Integer.MIN_VALUE; // Traverse all subsets of the array for (int i = 0; i < (1 << n); i++) { // __builtin_popcount() returns the number // of sets bits in an integer if (Integer.bitCount(i) == k) { // Initialize current xor as 0 int cur_xor = 0; for (int j = 0; j < n; j++) { // If jth bit is set in i then include // jth element in the current xor if ((i & (1 << j)) == 0) cur_xor = cur_xor ^ arr[j]; } // Update maximum xor so far maxXor = Math.max(maxXor, cur_xor); } } return maxXor; } // Driver code public static void main(String[] args) { int arr[] = { 2, 5, 4, 1, 3, 7, 6, 8 }; int n = arr.length; int k = 3; System.out.println(Max_Xor(arr, n, k)); } } // This code is contributed by PrinciRaj1992 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum xor for a // subset of size k from the given array static int Max_Xor(int []arr, int n, int k) { // Initialize result int maxXor = int.MinValue; // Traverse all subsets of the array for (int i = 0; i < (1 << n); i++) { // __builtin_popcount() returns the number // of sets bits in an integer if (bitCount(i) == k) { // Initialize current xor as 0 int cur_xor = 0; for (int j = 0; j < n; j++) { // If jth bit is set in i then include // jth element in the current xor if ((i & (1 << j)) == 0) cur_xor = cur_xor ^ arr[j]; } // Update maximum xor so far maxXor = Math.Max(maxXor, cur_xor); } } return maxXor; } static int bitCount(long x) { int setBits = 0; while (x != 0) { x = x & (x - 1); setBits++; } return setBits; } // Driver code public static void Main(String[] args) { int []arr = { 2, 5, 4, 1, 3, 7, 6, 8 }; int n = arr.Length; int k = 3; Console.WriteLine(Max_Xor(arr, n, k)); } } // This code is contributed by Princi Singh |



