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.
C++
#include<bits/stdc++.h>
using namespace std;
int max_xor( int A[], int B[], int N)
{
int maximum;
for ( int i=0;i<N;i++)
{
maximum=INT_MIN;
for ( int j=0;j<N;j++)
{
maximum=max(maximum,A[i]^B[j]);
}
cout<<maximum<<endl;
}
}
int main()
{
int A[] = {7, 3, 9, 12};
int B[] = {1, 3, 5, 2};
int N = sizeof (A)/ sizeof (A[0]);
max_xor(A, B, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void max_xor( int A[], int B[], int N)
{
int maximum;
for ( int i= 0 ;i<N;i++)
{
maximum=Integer.MIN_VALUE;
for ( int j= 0 ;j<N;j++)
{
maximum=Math.max(maximum,A[i]^B[j]);
}
System.out.println(maximum);
}
}
public static void main (String[] args)
{
int A[] = { 7 , 3 , 9 , 12 };
int B[] = { 1 , 3 , 5 , 2 };
int N = A.length;
max_xor(A, B, N);
}
}
|
Python3
def max_xor(A, B, N):
maximum = - float ( 'inf' )
for i in range (N):
maximum = - float ( 'inf' )
for j in range (N):
maximum = max (maximum, A[i] ^ B[j])
print (maximum)
if __name__ = = '__main__' :
A = [ 7 , 3 , 9 , 12 ]
B = [ 1 , 3 , 5 , 2 ]
N = len (A)
max_xor(A, B, N)
|
C#
using System;
class Program {
static int MaxXOR( int [] A, int [] B, int N)
{
int maximum;
for ( int i = 0; i < N; i++) {
maximum = int .MinValue;
for ( int j = 0; j < N; j++) {
maximum = Math.Max(maximum, A[i] ^ B[j]);
}
Console.WriteLine(maximum);
}
return 0;
}
static void Main()
{
int [] A = { 7, 3, 9, 12 };
int [] B = { 1, 3, 5, 2 };
int N = A.Length;
MaxXOR(A, B, N);
}
}
|
Javascript
function max_xor(A, B) {
let maximum;
for (let i = 0; i < A.length; i++) {
maximum = Number.MIN_SAFE_INTEGER;
for (let j = 0; j < B.length; j++) {
maximum = Math.max(maximum, A[i] ^ B[j]);
}
console.log(maximum);
}
}
let A = [7, 3, 9, 12];
let B = [1, 3, 5, 2];
max_xor(A, B);
|
Time Complexity : O(n^2)
Space Complexity : O(1)
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 implementation
C++
#include<bits/stdc++.h>
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;
}
int max_xor(trie * root, int key)
{
trie * temp = root;
for ( int i = 31; i >= 0; i--)
{
bool current_bit = ( key & ( 1 << i) );
if (temp -> child[1 - current_bit] != NULL)
temp = temp -> child[1 - current_bit];
else
temp = temp -> child[current_bit];
}
return (key ^ temp -> value);
}
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 main()
{
int A[] = {7, 3, 9, 12};
int B[] = {1, 3, 5, 2};
int N = sizeof (A)/ sizeof (A[0]);
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;
}
|
Java
import java.util.*;
class GFG
{
static class trie
{
int value;
trie []child = new trie[ 2 ];
};
static trie get()
{
trie root = new trie();
root.value = 0 ;
root.child[ 0 ] = null ;
root.child[ 1 ] = null ;
return root;
}
static int max_xor(trie root, int key)
{
trie temp = root;
for ( int i = 31 ; i >= 0 ; i--)
{
int current_bit = (key & ( 1 << i));
if (current_bit > 0 )
current_bit = 1 ;
if (temp.child[ 1 - current_bit] != null )
temp = temp.child[ 1 - current_bit];
else
temp = temp.child[current_bit];
}
return (key ^ temp.value);
}
static void insert(trie root, int key)
{
trie temp = root;
for ( int i = 31 ; i >= 0 ; i--)
{
int current_bit = key & ( 1 << i);
if (current_bit > 0 )
current_bit = 1 ;
if (temp.child[current_bit] == null )
temp.child[current_bit] = get();
temp = temp.child[current_bit];
}
temp.value = key;
}
public static void main(String[] args)
{
int A[] = { 7 , 3 , 9 , 12 };
int B[] = { 1 , 3 , 5 , 2 };
int N = A.length;
trie root = get();
for ( int i = 0 ; i < N; i++)
insert(root, B[i]);
for ( int i = 0 ; i < N; i++)
System.out.println(max_xor(root, A[i]));
}
}
|
Python3
class trie:
def __init__( self , value: int = 0 ) - > None :
self .value = value
self .child = [ None ] * 2
def get() - > trie:
root = trie()
root.value = 0
root.child[ 0 ] = None
root.child[ 1 ] = None
return root
def max_xor(root: trie, key: int ) - > int :
temp = root
for i in range ( 31 , - 1 , - 1 ):
current_bit = (key & ( 1 << i))
if (current_bit > 0 ):
current_bit = 1
if (temp.child[ 1 - current_bit] ! = None ):
temp = temp.child[ 1 - current_bit]
else :
temp = temp.child[current_bit]
return (key ^ temp.value)
def insert(root: trie, key: int ) - > None :
temp = root
for i in range ( 31 , - 1 , - 1 ):
current_bit = key & ( 1 << i)
if (current_bit > 0 ):
current_bit = 1
if (temp.child[current_bit] = = None ):
temp.child[current_bit] = get()
temp = temp.child[current_bit]
temp.value = key
if __name__ = = "__main__" :
A = [ 7 , 3 , 9 , 12 ]
B = [ 1 , 3 , 5 , 2 ]
N = len (A)
root = get()
for i in range (N):
insert(root, B[i])
for i in range (N):
print (max_xor(root, A[i]))
|
C#
using System;
class GFG
{
class trie
{
public int value;
public trie []child = new trie[2];
};
static trie get ()
{
trie root = new trie();
root.value = 0;
root.child[0] = null ;
root.child[1] = null ;
return root;
}
static int max_xor(trie root, int key)
{
trie temp = root;
for ( int i = 31; i >= 0; i--)
{
int current_bit = (key & (1 << i));
if (current_bit > 0)
current_bit = 1;
if (temp.child[1 - current_bit] != null )
temp = temp.child[1 - current_bit];
else
temp = temp.child[current_bit];
}
return (key ^ temp.value);
}
static void insert(trie root, int key)
{
trie temp = root;
for ( int i = 31; i >= 0; i--)
{
int current_bit = key & (1 << i);
if (current_bit > 0)
current_bit = 1;
if (temp.child[current_bit] == null )
temp.child[current_bit] = get ();
temp = temp.child[current_bit];
}
temp.value = key;
}
public static void Main(String[] args)
{
int []A = {7, 3, 9, 12};
int []B = {1, 3, 5, 2};
int N = A.Length;
trie root = get ();
for ( int i = 0; i < N; i++)
insert(root, B[i]);
for ( int i = 0; i < N; i++)
Console.WriteLine(max_xor(root, A[i]));
}
}
|
Javascript
<script>
class trie
{
constructor()
{
this .value=0;
this .child = new Array(2);
}
}
function get()
{
let root = new trie();
root.value = 0;
root.child[0] = null ;
root.child[1] = null ;
return root;
}
function max_xor(root,key)
{
let temp = root;
for (let i = 31; i >= 0; i--)
{
let current_bit = (key & (1 << i));
if (current_bit > 0)
current_bit = 1;
if (temp.child[1 - current_bit] != null )
temp = temp.child[1 - current_bit];
else
temp = temp.child[current_bit];
}
return (key ^ temp.value);
}
function insert(root,key)
{
let temp = root;
for (let i = 31; i >= 0; i--)
{
let current_bit = key & (1 << i);
if (current_bit > 0)
current_bit = 1;
if (temp.child[current_bit] == null )
temp.child[current_bit] = get();
temp = temp.child[current_bit];
}
temp.value = key;
}
let A=[7, 3, 9, 12];
let B=[1, 3, 5, 2];
let N = A.length;
let root = get();
for (let i = 0; i < N; i++)
insert(root, B[i]);
for (let i = 0; i < N; i++)
document.write(max_xor(root, A[i])+ "<br>" );
</script>
|
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)
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...