Maximum XOR of k Size Subset
Last Updated :
12 Jul, 2025
Given an integer array arr[] of size n and an integer k. The task is to find the maximum XOR of the subset of size k from the given array arr[].
Examples:
Input: arr[] = [2, 5, 4, 1, 3, 7, 6, 8], k = 3
Output: 15
Explanation: XOR of the elements of subset {2, 5, 8} is 15, which is the maximum possible.
Input: arr[] = [3, 4, 7, 7, 9], k = 3
Output: 14
Explanation: XOR of the elements of subset {3, 4, 9} is 14, which is the maximum possible.
Input: arr[] = [3, 4, 7, 16, 9], k = 1
Output: 16
Explanation: We need to select subset of size 1, thus the element with maximum value is the answer, which is 16.
[Naive Approach] - Using Recursion - O(2 ^ n) Time and O(1) Space
The idea is to generate all possible subset of size k from the given array arr[], and find the one with XOR of its elements maximum. For each element, there two options, either include it into subset or exclude it. Firstly, exclude the element and move to the next one. Thereafter add the element in the current subset, and update the XOR. When the size of subset is k, check if the current XOR is greater than previous, if so, update the XOR value. At last return the maximum XOR.
Follow the below given step-by-step approach:
- Start with a recursive function that keeps track of the current index, the current XOR value, and the size of the current subset.
- If the size of the current subset reaches
k
, compare the XOR with the maximum XOR found so far and update if it's greater. - If the current index reaches the end of the array, return from the recursion.
- Recur by excluding the current element and moving to the next index.
- Recur by including the current element, updating the XOR and size of the subset, then moving to the next index.
- Continue this process until all combinations of subsets of size
k
have been explored. - Return the maximum XOR obtained among all valid subsets.
Below is given the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
// Recursive function to generate all
// subsets of size k and find the XOR
void findMaxXor(int ind, int curXOR, int curSize, int k,
int n, vector<int> arr, int &maxXOR) {
// If the current subset is of size k
if (curSize == k) {
maxXOR = max(maxXOR, curXOR);
return;
}
// If all elements are processed
if (ind == n) {
return;
}
// Include the current element
findMaxXor(ind + 1, curXOR ^ arr[ind],
curSize + 1, k, n, arr, maxXOR);
// Exclude the current element
findMaxXor(ind + 1, curXOR,
curSize, k, n, arr, maxXOR);
}
// Function to return the maximum xor for a
// subset of size k from the given array
int maxKSubset(vector<int> arr, int k) {
int n = arr.size();
// to store the maximum xor
int maxXOR = INT_MIN;
// Traverse all subsets of the array
findMaxXor(0, 0, 0, k, n, arr, maxXOR);
return maxXOR;
}
int main() {
vector<int> arr = { 2, 5, 4, 1, 3, 7, 6, 8 };
int k = 3;
cout<<maxKSubset(arr, k);
return 0;
}
Java
// Recursive function to generate all
// subsets of size k and find the XOR
import java.util.*;
class GfG {
// Recursive function to generate all
// subsets of size k and find the XOR
static void findMaxXor(int ind, int curXOR,
int curSize, int k, int n, int[] arr, int[] maxXOR) {
// If the current subset is of size k
if (curSize == k) {
maxXOR[0] = Math.max(maxXOR[0], curXOR);
return;
}
// If all elements are processed
if (ind == n) {
return;
}
// Include the current element
findMaxXor(ind + 1, curXOR ^ arr[ind],
curSize + 1, k, n, arr, maxXOR);
// Exclude the current element
findMaxXor(ind + 1, curXOR,
curSize, k, n, arr, maxXOR);
}
// Function to return the maximum xor for a
// subset of size k from the given array
static int maxKSubset(int[] arr, int k) {
int n = arr.length;
// to store the maximum xor
int[] maxXOR = new int[1];
maxXOR[0] = Integer.MIN_VALUE;
// Traverse all subsets of the array
findMaxXor(0, 0, 0, k, n, arr, maxXOR);
return maxXOR[0];
}
public static void main(String[] args) {
int[] arr = {2, 5, 4, 1, 3, 7, 6, 8};
int k = 3;
System.out.println(maxKSubset(arr, k));
}
}
Python
# Recursive function to generate all
# subsets of size k and find the XOR
def findMaxXor(ind, curXOR, curSize, k, n, arr, maxXOR):
# If the current subset is of size k
if curSize == k:
maxXOR[0] = max(maxXOR[0], curXOR)
return
# If all elements are processed
if ind == n:
return
# Include the current element
findMaxXor(ind + 1, curXOR ^ arr[ind],
curSize + 1, k, n, arr, maxXOR)
# Exclude the current element
findMaxXor(ind + 1, curXOR,
curSize, k, n, arr, maxXOR)
# Function to return the maximum xor for a
# subset of size k from the given array
def maxKSubset(arr, k):
n = len(arr)
# to store the maximum xor
maxXOR = [float('-inf')]
# Traverse all subsets of the array
findMaxXor(0, 0, 0, k, n, arr, maxXOR)
return maxXOR[0]
if __name__ == "__main__":
arr = [2, 5, 4, 1, 3, 7, 6, 8]
k = 3
print(maxKSubset(arr, k))
C#
// Recursive function to generate all
// subsets of size k and find the XOR
using System;
using System.Collections.Generic;
class GfG {
// Recursive function to generate all
// subsets of size k and find the XOR
static void findMaxXor(int ind, int curXOR,
int curSize, int k, int n, int[] arr, ref int maxXOR) {
// If the current subset is of size k
if (curSize == k) {
maxXOR = Math.Max(maxXOR, curXOR);
return;
}
// If all elements are processed
if (ind == n) {
return;
}
// Include the current element
findMaxXor(ind + 1, curXOR ^ arr[ind],
curSize + 1, k, n, arr, ref maxXOR);
// Exclude the current element
findMaxXor(ind + 1, curXOR,
curSize, k, n, arr, ref maxXOR);
}
// Function to return the maximum xor for a
// subset of size k from the given array
static int maxKSubset(int[] arr, int k) {
int n = arr.Length;
// to store the maximum xor
int maxXOR = int.MinValue;
// Traverse all subsets of the array
findMaxXor(0, 0, 0, k, n, arr, ref maxXOR);
return maxXOR;
}
static void Main() {
int[] arr = {2, 5, 4, 1, 3, 7, 6, 8};
int k = 3;
Console.WriteLine(maxKSubset(arr, k));
}
}
JavaScript
// Recursive function to generate all
// subsets of size k and find the XOR
function findMaxXor(ind, curXOR, curSize, k, n, arr, maxXOR) {
// If the current subset is of size k
if (curSize === k) {
maxXOR[0] = Math.max(maxXOR[0], curXOR);
return;
}
// If all elements are processed
if (ind === n) {
return;
}
// Include the current element
findMaxXor(ind + 1, curXOR ^ arr[ind],
curSize + 1, k, n, arr, maxXOR);
// Exclude the current element
findMaxXor(ind + 1, curXOR,
curSize, k, n, arr, maxXOR);
}
// Function to return the maximum xor for a
// subset of size k from the given array
function maxKSubset(arr, k) {
let n = arr.length;
// to store the maximum xor
let maxXOR = [Number.NEGATIVE_INFINITY];
// Traverse all subsets of the array
findMaxXor(0, 0, 0, k, n, arr, maxXOR);
return maxXOR[0];
}
let arr = [2, 5, 4, 1, 3, 7, 6, 8];
let k = 3;
console.log(maxKSubset(arr, k));
[Expected Approach] - Using Memoization
The above approach can be optimized using Memoization. The idea is to store the results in a 3d array memo, to avoid computing the same subproblem multiple times. Create an array memo of order n * k * x, where n is the size of array, k is the size of subset, and x is the maximum XOR value (we are using unordered map). Here memo[i][j][x] stores the maximum possible XOR such that there are j elements in the subset up to index i with XOR x.
Follow the below given step-by-step approach:
- Use a 3D memoization structure
memo[i][j][x]
, where:i
is the current index in the array,j
is the current size of the subset being formed,x
is the current XOR value.
- Initialize the memoization structure using an unordered map to dynamically manage the possible XOR values.
- Define a recursive function that explores all combinations by either:
- Skipping the current element, or
- Including the current element, updating the XOR value and the count.
- Before processing a state, check if it already exists in the memoization structure. If it does, return the stored result.
- If the size of the subset becomes
k
, return the current XOR value as a potential candidate for the maximum. - Continue exploring other combinations and update the memoization structure with the maximum XOR obtained from the current state.
- Finally, return the highest XOR value recorded during the exploration.
Below is given the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxXor(int ind, int curXOR, int curSize, int k, int n,
vector<int> &arr, vector<vector<unordered_map<int, int>>> &memo) {
// If the current subset is of size k
if (curSize == k) {
return curXOR;
}
// If all elements are processed
if (ind == n) {
return 0;
}
// If the value is already calculated
if (memo[ind][curSize].count(curXOR) != 0) {
return memo[ind][curSize][curXOR];
}
// Include the current element
int include = findMaxXor(ind + 1, curXOR ^ arr[ind],
curSize + 1, k, n, arr, memo);
// Exclude the current element
int exclude = findMaxXor(ind + 1,
curXOR, curSize, k, n, arr, memo);
// Store the maximum value
return memo[ind][curSize][curXOR] = max(include, exclude);
}
// Function to return the maximum xor for a
// subset of size k from the given array
int maxKSubset(vector<int> arr, int k) {
int n = arr.size();
// create a 3d dp to store the answer
vector<vector<unordered_map<int, int>>> memo(n + 1,
vector<unordered_map<int, int>>(k + 1));
// Traverse all subsets of the array
// and return the maximum xor
return findMaxXor(0, 0, 0, k, n, arr, memo);
}
int main() {
vector<int> arr = { 2, 5, 4, 1, 3, 7, 6, 8 };
int k = 3;
cout<<maxKSubset(arr, k);
return 0;
}
Java
// Recursive function to generate all
// subsets of size k and find the XOR
import java.util.*;
class GfG {
// Recursive function to generate all
// subsets of size k and find the XOR
static int findMaxXor(int ind, int curXOR, int curSize, int k,
int n, int[] arr, ArrayList<ArrayList<HashMap<Integer, Integer>>> memo) {
// If the current subset is of size k
if (curSize == k) {
return curXOR;
}
// If all elements are processed
if (ind == n) {
return 0;
}
// If the value is already calculated
if (memo.get(ind).get(curSize).containsKey(curXOR)) {
return memo.get(ind).get(curSize).get(curXOR);
}
// Include the current element
int include = findMaxXor(ind + 1,
curXOR ^ arr[ind], curSize + 1, k, n, arr, memo);
// Exclude the current element
int exclude = findMaxXor(ind + 1,
curXOR, curSize, k, n, arr, memo);
// Store the maximum value
int val = Math.max(include, exclude);
memo.get(ind).get(curSize).put(curXOR, val);
return val;
}
// Function to return the maximum xor for a
// subset of size k from the given array
static int maxKSubset(int[] arr, int k) {
int n = arr.length;
// create a 3d dp to store the answer
ArrayList<ArrayList<HashMap<Integer, Integer>>> memo =
new ArrayList<>();
for (int i = 0; i <= n; i++) {
ArrayList<HashMap<Integer, Integer>> list =
new ArrayList<>();
for (int j = 0; j <= k; j++) {
list.add(new HashMap<>());
}
memo.add(list);
}
// Traverse all subsets of the array
// and return the maximum xor
return findMaxXor(0, 0, 0, k, n, arr, memo);
}
public static void main(String[] args) {
int[] arr = {2, 5, 4, 1, 3, 7, 6, 8};
int k = 3;
System.out.println(maxKSubset(arr, k));
}
}
Python
# Recursive function to generate all
# subsets of size k and find the XOR
def findMaxXor(ind, curXOR, curSize, k, n, arr, memo):
# If the current subset is of size k
if curSize == k:
return curXOR
# If all elements are processed
if ind == n:
return 0
# If the value is already calculated
if curXOR in memo[ind][curSize]:
return memo[ind][curSize][curXOR]
# Include the current element
include = findMaxXor(ind + 1, curXOR ^ arr[ind],
curSize + 1, k, n, arr, memo)
# Exclude the current element
exclude = findMaxXor(ind + 1, curXOR,
curSize, k, n, arr, memo)
# Store the maximum value
val = max(include, exclude)
memo[ind][curSize][curXOR] = val
return val
# Function to return the maximum xor for a
# subset of size k from the given array
def maxKSubset(arr, k):
n = len(arr)
# create a 3d dp to store the answer
memo = [[{} for _ in range(k + 1)] for _ in range(n + 1)]
# Traverse all subsets of the array
# and return the maximum xor
return findMaxXor(0, 0, 0, k, n, arr, memo)
if __name__ == "__main__":
arr = [2, 5, 4, 1, 3, 7, 6, 8]
k = 3
print(maxKSubset(arr, k))
C#
// Recursive function to generate all
// subsets of size k and find the XOR
using System;
using System.Collections.Generic;
class GfG {
// Recursive function to generate all
// subsets of size k and find the XOR
static int findMaxXor(int ind, int curXOR, int curSize,
int k, int n, int[] arr, List<List<Dictionary<int, int>>> memo) {
// If the current subset is of size k
if (curSize == k) {
return curXOR;
}
// If all elements are processed
if (ind == n) {
return 0;
}
// If the value is already calculated
if (memo[ind][curSize].ContainsKey(curXOR)) {
return memo[ind][curSize][curXOR];
}
// Include the current element
int include = findMaxXor(ind + 1,
curXOR ^ arr[ind], curSize + 1, k, n, arr, memo);
// Exclude the current element
int exclude = findMaxXor(ind + 1,
curXOR, curSize, k, n, arr, memo);
// Store the maximum value
int val = Math.Max(include, exclude);
memo[ind][curSize][curXOR] = val;
return val;
}
// Function to return the maximum xor for a
// subset of size k from the given array
static int maxKSubset(int[] arr, int k) {
int n = arr.Length;
// create a 3d dp to store the answer
List<List<Dictionary<int, int>>> memo =
new List<List<Dictionary<int, int>>>();
for (int i = 0; i <= n; i++) {
List<Dictionary<int, int>> list =
new List<Dictionary<int, int>>();
for (int j = 0; j <= k; j++) {
list.Add(new Dictionary<int, int>());
}
memo.Add(list);
}
// Traverse all subsets of the array
// and return the maximum xor
return findMaxXor(0, 0, 0, k, n, arr, memo);
}
static void Main() {
int[] arr = {2, 5, 4, 1, 3, 7, 6, 8};
int k = 3;
Console.WriteLine(maxKSubset(arr, k));
}
}
JavaScript
// Recursive function to generate all
// subsets of size k and find the XOR
function findMaxXor(ind, curXOR, curSize, k, n, arr, memo) {
// If the current subset is of size k
if (curSize === k) {
return curXOR;
}
// If all elements are processed
if (ind === n) {
return 0;
}
// If the value is already calculated
if (memo[ind][curSize].hasOwnProperty(curXOR)) {
return memo[ind][curSize][curXOR];
}
// Include the current element
let include = findMaxXor(ind + 1,
curXOR ^ arr[ind], curSize + 1, k, n, arr, memo);
// Exclude the current element
let exclude = findMaxXor(ind + 1,
curXOR, curSize, k, n, arr, memo);
// Store the maximum value
let val = Math.max(include, exclude);
memo[ind][curSize][curXOR] = val;
return val;
}
// Function to return the maximum xor for a
// subset of size k from the given array
function maxKSubset(arr, k) {
let n = arr.length;
// create a 3d dp to store the answer
let memo = [];
for (let i = 0; i <= n; i++) {
memo.push([]);
for (let j = 0; j <= k; j++) {
memo[i].push({});
}
}
// Traverse all subsets of the array
// and return the maximum xor
return findMaxXor(0, 0, 0, k, n, arr, memo);
}
let arr = [2, 5, 4, 1, 3, 7, 6, 8];
let k = 3;
console.log(maxKSubset(arr, k));
Time Complexity: O(n * k * x), where n is the size of array, k is the size of subset, and x is the maximum possible XOR.
Auxiliary Space: O(n * k * x)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem