Maximum possible XOR of every element in an array with another array in C++


In this problem, we are given two arrays A and B of n elements each. Our task is to create a program to find the maximum possible XOR of every element in an array with another array.

We have to compute the maximum XOR for each element of array A with array B i.e. for each element of array A we will select an element in array B which will have the maximum XOR value.

Let's take an example to understand the problem −

Input

array A = {3, 6 ,11, 9}
array B = {8, 2, 4, 1}

Output

11 14 15 13

Explanation

Let’s see the XOR combination of each element of array A with all elements of array B and then select the maximum for each.

3 XOR 8 = 11 3 XOR 2 = 1 3 XOR 4 = 7 3 XOR 1 = 2
Maximum = 11.
6 XOR 8 = 14 6 XOR 2 = 4 6 XOR 4 = 2 6 XOR 1 = 1
Maximum = 14.
11 XOR 8 = 3 11 XOR 2 = 9 11 XOR 4 = 15 11 XOR 1 = 10
Maximum = 15.
9 XOR 8 = 1 9 XOR 2 = 11 9 XOR 4 = 13 9 XOR 1 = 8
Maximum = 13.

To solve this problem, a simple and naive approach is to calculate all the combinations and print the maximum XOR as shown in the above example.

But this will not be effective as the code relies on two loops which make its complexity of the order O(n^2).

So, we will see a better solution to the problem.

It is to use a trie data structure which will store the binary of all elements of array B for the match with array A, to find the maximum XOR.

So, for an element of array A, we will check its most significant bit and try to make it 1. And the go to the next MSB. Following this we will get our maximum XOR element for an element of A in array B.

Example

Program to find the maximum possible XOR of every element in an array with another array

 Live Demo

#include<iostream>
using namespace std;
struct trie{
   int value;
   trie *child[2];
};
trie * get(){
   trie * root = new trie;
   root -> value = 0;
   root -> child[0] = NULL;
   root -> child[1] = NULL;
   return root;
}
void insert(trie * root, int key){
   trie * temp = root;
   for (int i = 31; i >= 0; i--){
      bool current_bit = key & (1 << i);
      if (temp -> child[current_bit] == NULL)
         temp -> child[current_bit] = get();
      temp = temp -> child[current_bit];
   }
   temp -> value = key;
}
int findMaxXor(trie * root, int element){
   trie * temp = root;
   for (int i = 31; i >= 0; i--){
      bool bits = ( element & ( 1 << i) );
      if (temp -> child[1 - bits] != NULL)
         temp = temp -> child[1 - bits];
      else
         temp = temp -> child[bits];
   }
   return (element ^ temp -> value);
}
int main(){
   int A[] = {3, 11, 6, 9};
   int B[] = {8, 2, 4, 1};
   int N = sizeof(A)/sizeof(A[0]);
   trie * root = get();
   for (int i = 0; i < N; i++)
   insert(root, B[i]);
   cout<<"The maximum possible XOR of every possible element in array A with Array B is\n";
   for (int i = 0; i < N; i++)
   cout <<findMaxXor(root, A[i])<<"\t";
   return 0;
}

Output

The maximum possible XOR of every possible element in array A with Array B is
11 15 14 13

Updated on: 03-Jun-2020

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements