Maximum possible XOR of every element in an array with another array
Two arrays A and B consisting of N elements are given. The task is to compute the maximum possible XOR of every element in array A with array B.
Examples:
Input : A : 7 3 9 12 B : 1 3 5 2 Output : 6 6 12 15 Explanation : 1 xor 7 = 6, 5 xor 3 = 6, 5 xor 9 = 12, 3 xor 12 = 15.
A naive approach to solve this problem would be to check every element of array A with every other element of array B and print the maximum xor.
Time Complexity : O(n^2)
An efficient solution is to use Trie Data Structure.
We maintain a Trie for the binary representation of all elements in array B.
Now, for every element of A, we find the maximum xor in trie.
Let’s say our number A[i] is {b1, b2…bn}, where b1, b2…bn are binary bits. We start from b1.
Now for the xor to be maximum, we’ll try to make most significant bit 1 after performing
the xor. So, if b1 is 0, we’ll need a 1 and vice versa. In the trie, we go to the required
bit side. If favourable option is not there, we’ll go other side. Doing this all over,
we’ll get the maximum XOR possible.
Below is the C++ implementation
// CPP code to find the maximum possible X-OR of // every array element with another array #include<bits/stdc++.h> using namespace std; // Structure of Trie DS struct trie { int value; trie *child[2]; }; // Function to initialise a Trie Node trie * get() { trie * root = new trie; root -> value = 0; root -> child[0] = NULL; root -> child[1] = NULL; return root; } // Computing max xor int max_xor(trie * root, int key) { trie * temp = root; // Checking for all bits in integer range for (int i = 31; i >= 0; i--) { // Current bit in the number bool current_bit = ( key & ( 1 << i) ); // Traversing Trie for different bit, if found if (temp -> child[1 - current_bit] != NULL) temp = temp -> child[1 - current_bit]; // Traversing Trie for same bit else temp = temp -> child[current_bit]; } // Returning xor value of maximum bit difference // value. Thus, we get maximum xor value return (key ^ temp -> value); } // Inserting B[] in Trie void insert(trie * root, int key) { trie * temp = root; // Storing 31 bits as integer representation for (int i = 31; i >= 0; i--) { // Current bit in the number bool current_bit = key & (1 << i); // New node required if (temp -> child[current_bit] == NULL) temp -> child[current_bit] = get(); // Traversing in Trie temp = temp -> child[current_bit]; } // Assigning value to the leaf Node temp -> value = key; } int main() { int A[] = {7, 3, 9, 12}; int B[] = {1, 3, 5, 2}; int N = sizeof(A)/sizeof(A[0]); // Forming Trie for B[] trie * root = get(); for (int i = 0; i < N; i++) insert(root, B[i]); for (int i = 0; i < N; i++) cout << max_xor(root, A[i]) << endl; return 0; } |
Output:
6 6 12 15
Trie formation : O(N x MAX_BITS) where MAX_BITS is maximum number of bits in binary representation of numbers.
Calculating max bit difference : O(N x MAX_BITS)
Time Complexity : O(N x MAX_BITS)
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Maximum element in an array such that its previous and next element product is maximum
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Type of array and its maximum element
- Remove all occurrences of any element for maximum array sum
- Maximum difference between first and last indexes of an element in array
- Maximum element in a sorted and rotated array
- Maximum distance between two occurrences of same element in array
- Find maximum sum taking every Kth element in the array
- Find element with the maximum set bits in an array
- Sum and Product of minimum and maximum element of an Array
- Minimum and Maximum element of an array which is divisible by a given number k
- Find maximum value of the last element after reducing the array with given operations
- Find the maximum element in an array which is first increasing and then decreasing
- Replace the maximum element in the array by coefficient of range



