Maximum XOR Bitmask Subset
Problem Description
Given an integer array `elements`, find the **maximum XOR value** achievable by XOR-ing together any **non-empty subset** of `elements`.
Recall that a subset can include any combination of the array's values (each used at most once).
**Example 1:**
```
Input: elements = [3, 10, 5, 25, 2, 8]
Output: 28
Explanation: The subset {5, 25} gives 5 XOR 25 = 28, which is the maximum achievable.
```
**Example 2:**
```
Input: elements = [14, 7, 3, 6]
Output: 15
Explanation: The subset {14, 1} — wait, 1 is not in the array.
7 XOR 14 = 9, 3 XOR 14 = 13, 6 XOR 14 = 8, 7 XOR 6 = 1, 3 XOR 6 = 5,
3 XOR 7 = 4, 14 XOR 7 XOR 3 XOR 6 = 14^7=9,9^3=10,10^6=12.
Single elements: max is 14. But 7^6^3 = 7^6=1,1^3=2. 14^3=13. 14^6^3=14^5=11. 14^7^6=14^1=15 ✓.
Output: 15
```
**Example 2:**
```
Input: elements = [14, 7, 3, 6]
Output: 15
Explanation: XOR-ing {14, 7, 6} → 14 XOR 7 XOR 6 = 15.
```
Constraints
- 1 <= elements.length <= 200
- 0 <= elements[i] <= 10^9
Follow-up
If the array can have up to 10^5 elements and values up to 2^63-1, can you still solve it in O(64n) time using the same linear basis technique with 64-bit integers?
Hints
Try the problem first. If you get stuck, you can reveal hints one at a time.
Solution
Leaderboard
No entries yet for python.
Be the first — submit an accepted solution.