Skip to main content

Maximum XOR of Subsequences - Solution & Explanation

Practice this problem

Problem Statement

You are given an integer array nums of length n where each element is a non-negative integer.

Select two subsequences of nums (they may be empty and are allowed to overlap), each preserving the original order of elements, and let:

  • X be the bitwise XOR of all elements in the first subsequence.
  • Y be the bitwise XOR of all elements in the second subsequence.

Return the maximum possible value of X XOR Y.

Note: The XOR of an empty subsequence is 0.

 

Example 1:

Input: nums = [1,2,3]

Output: 3

Explanation:

Choose subsequences:

  • First subsequence [2], whose XOR is 2.
  • Second subsequence [2,3], whose XOR is 1.

Then, XOR of both subsequences = 2 XOR 1 = 3.

This is the maximum XOR value achievable from any two subsequences.

Example 2:

Input: nums = [5,2]

Output: 7

Explanation:

Choose subsequences:

  • First subsequence [5], whose XOR is 5.
  • Second subsequence [2], whose XOR is 2.

Then, XOR of both subsequences = 5 XOR 2 = 7.

This is the maximum XOR value achievable from any two subsequences.

 

Constraints:

  • 2 <= nums.length <= 105
  • 0 <= nums[i] <= 109

Approach Overview

Problem Overview: You are given an integer array and must choose any subsequence whose XOR value is as large as possible. The task is to determine the maximum XOR achievable by selecting any subset of elements while preserving their order.

Approach 1: Brute Force Subsequence Enumeration (O(2^n) time, O(1) space)

The most direct method is to try every possible subsequence. For each subset of indices, compute the XOR of the selected numbers and track the maximum value. This works because XOR is associative and easy to compute incrementally. However, the number of subsequences is 2^n, which becomes infeasible once n grows beyond ~25. This approach is useful only for understanding the problem or verifying small test cases.

Approach 2: Dynamic Programming on XOR States (O(n * M) time, O(M) space)

Another idea is to track all reachable XOR values while iterating through the array. Maintain a set or boolean DP array where each state represents a possible XOR result. For every number x, update the states by inserting state ^ x. This effectively simulates choosing or skipping each element. The limitation is that the number of XOR states can grow up to the maximum value range (M), which becomes large when integers have many bits. This approach works when values are small but struggles with typical competitive programming constraints.

Approach 3: Greedy XOR Linear Basis (O(n * B) time, O(B) space)

The optimal strategy treats the problem as a maximum subset XOR problem using a linear basis over bits. Iterate through the array and maintain a basis where each element represents a number with a unique highest set bit. For every new value, reduce it using existing basis vectors (XOR with them if it lowers the value). If the result is non-zero, insert it into the basis. This effectively performs Gaussian elimination in binary space.

Once the basis is built, compute the maximum XOR by greedily combining basis elements from highest bit to lowest. Each step attempts ans = max(ans, ans ^ basis[i]). The basis guarantees independence between vectors, allowing you to construct the maximum possible XOR value.

This method relies heavily on Bit Manipulation and a greedy property of XOR linear independence. It is widely used in problems involving maximum subset XOR and XOR optimization on Array data. The elimination process is conceptually similar to techniques used in Math problems involving binary vector spaces.

Recommended for interviews: The XOR linear basis approach is the expected solution. Brute force shows you understand subsequences and XOR behavior, but the greedy basis demonstrates deeper knowledge of bit manipulation and linear independence—something interviewers often look for in hard problems.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subsequence EnumerationO(2^n)O(1)Small arrays or for verifying correctness during testing
Dynamic Programming on XOR StatesO(n * M)O(M)When values are small and XOR state space is limited
Greedy XOR Linear BasisO(n * B)O(B)General case with large values; optimal competitive programming solution

Video Solution

Leetcode 3681 | Maximum XOR of Subsequences • CodeWithMeGuys • 746 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Maximum XOR of Subsequences easy or hard?
The problem is typically classified as Hard because it requires knowledge of XOR linear basis or binary Gaussian elimination. Without this technique, brute force or naive dynamic programming approaches quickly become infeasible.
Maximum XOR of Subsequences Python/Java solution
Most implementations maintain a basis array of size equal to the bit width of integers. For each number, repeatedly XOR it with existing basis vectors to reduce it. If the final value is non‑zero, store it in the basis. The same logic works in Python, Java, C++, and Go using standard bitwise operations.
How to solve Maximum XOR of Subsequences in O(n)?
Use a greedy XOR linear basis. Iterate through the array and insert each number into the basis using bitwise elimination (similar to Gaussian elimination). Once the basis is built, greedily construct the largest XOR by combining basis elements from highest bit to lowest. Because the bit count is constant, the runtime behaves like O(n).
What is the best approach for Maximum XOR of Subsequences?
The optimal approach uses a XOR linear basis (also called a basis over GF(2)). Each number is reduced against existing basis vectors and inserted only if it introduces a new independent bit. After building the basis, greedily combine vectors to maximize the XOR value. This runs in O(n * B) time where B is the number of bits (usually 31 or 64).
Is Maximum XOR of Subsequences asked at Google/Amazon/Meta?
Variants of the maximum subset XOR problem appear in interviews at companies like Google, Amazon, and Meta. These questions test understanding of bit manipulation, greedy strategies, and linear basis techniques used in competitive programming.
What data structure is used in Maximum XOR of Subsequences?
The main structure is a XOR linear basis stored as an array indexed by bit position. Each entry represents a vector with a unique highest set bit. The structure allows efficient insertion and reduction operations using bit manipulation.
What is the time complexity of Maximum XOR of Subsequences?
The optimal linear basis solution runs in O(n * B) time and O(B) space, where n is the array size and B is the number of bits in the integers. Since B is typically at most 31 or 64, the algorithm is effectively linear in practice.

Ready to solve this problem?

Practice Maximum XOR of Subsequences with our built-in code editor and test cases.

Practice on FleetCode