![]() |
VOOZH | about |
Given an array arr[] of positive integers, find the maximum XOR value obtainable by taking the XOR of any subset of its elements.
Examples:
Input: arr[] = [2, 4, 5]
Output: 7
Explanation: The subset {2, 5} has the maximum XOR value.Input: arr[] = [9, 8, 5]
Output: 13
Explanation: The subset {8, 5} has the maximum XOR value.
Table of Content
Generate all subsets, compute the XOR of elements in each subset, and keep track of the maximum XOR value obtained.
Since a subset can either include or exclude each element, there are 2n possible subsets of an array of size n.
7
The idea is based on below facts:
- Number of bits to represent all elements is fixed which is 32 bits for integer in most of the cases.
- If an element has Most Significant Bit MSB at position i, then result is at least 2i
Steps
7
Time Complexity: O(n × log(max(arr))) - For each bit position, all n elements may be processed once.
Auxiliary Space: O(1)