Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
Given a perfect binary tree of N nodes, with nodes having values from 1 to N as depicted in the image below and Q queries where every query consists of two integers L and X. The task is to find the maximum possible value of X XOR Y where Y can be any node at level L.
Examples:
Input: Q[] = {{2, 5}, {3, 15}}
Output:
7
11
1st Query: Level 2 has numbers 2 and 3.
Therefore, 2^5 = 7 and 3^5 = 6.
Hence, the answer is 7.
2nd Query: Level 3 has numbers 4, 5, 6 and 7
and 4^15 = 11 is the maximum possible.
Input: Q[] = {{ 1, 15 }, { 5, 23 }}
Output:
14
15
Approach: The numbers in level L contains L bits, e.g, in level 2 the numbers are 2 and 3 which can be represented using 2 bits in binary. Similarly, in level 3 the numbers are 4, 5, 6 and 7 can be represented with 3 bits.
So we have to find an L bit number which gives maximum xor with X. Store the bits of X in an array a[]. Now, fill an array b[] of size L with elements opposite to that in a[], i.e., if a[i] is equal to 0 then put b[i] equal to 1 and vice-versa.
Note that the L-1 index of b[]must always be 1. If size of a[] is less than b[] then fill the remaining indexes of b[] with 1. The array b[] is filled opposite to that of array a[] so that the number made from the bits of array b[] gives maximum xor value. Lastly, calculate the number made from b[] and return its xor with X as the answer to the query.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 60
int solveQuery( int L, int X)
{
int res;
int a[MAXN], b[L];
int ref = X, size_a = 0;
while (ref > 0) {
a[size_a] = ref % 2;
ref /= 2;
size_a++;
}
for ( int i = 0; i < min(size_a, L); i++) {
if (a[i] == 1)
b[i] = 0;
else
b[i] = 1;
}
for ( int i = min(size_a, L); i < L; i++)
b[i] = 1;
b[L - 1] = 1;
int temp = 0, p = 1;
for ( int i = 0; i < L; i++) {
temp += b[i] * p;
p *= 2;
}
res = temp ^ X;
return res;
}
int main()
{
int queries[][2] = { { 2, 5 }, { 3, 15 } };
int q = sizeof (queries) / sizeof (queries[0]);
for ( int i = 0; i < q; i++)
cout << solveQuery(queries[i][0],
queries[i][1])
<< endl;
return 0;
}
|
Java
class GFG
{
static int MAXN = 60 ;
static int solveQuery( int L, int X)
{
int res;
int []a = new int [MAXN];
int []b = new int [L];
int refer = X, size_a = 0 ;
while (refer > 0 )
{
a[size_a] = refer % 2 ;
refer /= 2 ;
size_a++;
}
for ( int i = 0 ; i < Math.min(size_a, L); i++)
{
if (a[i] == 1 )
b[i] = 0 ;
else
b[i] = 1 ;
}
for ( int i = Math.min(size_a, L); i < L; i++)
b[i] = 1 ;
b[L - 1 ] = 1 ;
int temp = 0 , p = 1 ;
for ( int i = 0 ; i < L; i++)
{
temp += b[i] * p;
p *= 2 ;
}
res = temp ^ X;
return res;
}
static public void main (String args[])
{
int [][]queries= { { 2 , 5 }, { 3 , 15 } };
int q = queries.length;
for ( int i = 0 ; i < q; i++)
System.out.println( solveQuery(queries[i][ 0 ],
queries[i][ 1 ]) );
}
}
|
Python3
MAXN = 60
def solveQuery(L, X):
res = 0
a = [ 0 for i in range (MAXN)]
b = [ 0 for i in range (MAXN)]
ref = X
size_a = 0
while (ref > 0 ):
a[size_a] = ref % 2
ref / / = 2
size_a + = 1
for i in range ( min (size_a,L)):
if (a[i] = = 1 ):
b[i] = 0
else :
b[i] = 1
for i in range ( min (size_a, L),L):
b[i] = 1
b[L - 1 ] = 1
temp = 0
p = 1
for i in range (L):
temp + = b[i] * p
p * = 2
res = temp ^ X
return res
queries = [ [ 2 , 5 ], [ 3 , 15 ] ]
q = len (queries)
for i in range (q):
print (solveQuery(queries[i][ 0 ],queries[i][ 1 ]))
|
C#
using System;
class GFG
{
static int MAXN = 60;
static int solveQuery( int L, int X)
{
int res;
int []a = new int [MAXN];
int []b = new int [L];
int refer = X, size_a = 0;
while (refer > 0)
{
a[size_a] = refer % 2;
refer /= 2;
size_a++;
}
for ( int i = 0; i < Math.Min(size_a, L); i++)
{
if (a[i] == 1)
b[i] = 0;
else
b[i] = 1;
}
for ( int i = Math.Min(size_a, L); i < L; i++)
b[i] = 1;
b[L - 1] = 1;
int temp = 0, p = 1;
for ( int i = 0; i < L; i++)
{
temp += b[i] * p;
p *= 2;
}
res = temp ^ X;
return res;
}
static public void Main ()
{
int [,]queries= { { 2, 5 }, { 3, 15 } };
int q = queries.Length;
for ( int i = 0; i < q; i++)
Console.WriteLine( solveQuery(queries[i,0],
queries[i,1]) );
}
}
|
Javascript
<script>
var MAXN = 60;
function solveQuery(L, X)
{
var res;
var a = Array(MAXN), b = Array(L);
var ref = X, size_a = 0;
while (ref > 0)
{
a[size_a] = ref % 2;
ref = parseInt(ref / 2);
size_a++;
}
for ( var i = 0; i < Math.min(size_a, L); i++)
{
if (a[i] == 1)
b[i] = 0;
else
b[i] = 1;
}
for ( var i = Math.min(size_a, L); i < L; i++)
b[i] = 1;
b[L - 1] = 1;
var temp = 0, p = 1;
for ( var i = 0; i < L; i++)
{
temp += b[i] * p;
p *= 2;
}
res = temp ^ X;
return res;
}
var queries = [ [ 2, 5 ], [ 3, 15 ] ];
var q = queries.length;
for ( var i = 0; i < q; i++)
document.write(solveQuery(queries[i][0],
queries[i][1]) + "<br>" );
</script>
|
Time Complexity: O(q * MAXN)
Auxiliary Space: O(MAXN)
Last Updated :
18 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...