I am not able to understand the questions. Please explain the ques and approach
Didnt get the ques
Hey @hemantomer1
problem:
In this problem you will be given two inputs, lets say x and y. You have to find the maximum xor value of two numbers between x and y. For eg : user inputs x as 3 and y as 7. Then the numbers between 3 and 7, both included, are 3,4,5,6,7. You have to pair up all the numbers and find which pair has the max xor value. After finding the maximum xor value you have to print it. Just give it a try by yourself first, else refer to the solution approach below.
approach:
so basically the solution approach here is to write the binary representation of l and r given in the question.
now
1 xor 0 is 1
0 xor 1 is 1
0 xor 0 is 0
1 xor 1 is 0
so for xor of any two binary numbers to be maximum, maximum no of digits should be different.
Now we know R > L so We can see that first bit from L to R either changes from 0 to 1 or it stays 1 i.e. if we take the XOR of any two numbers for maximum value, their first bit will be fixed which will be same as first bit of XOR of L and R itself.
bcoz if first bit of L is 0 and R is 1, first bit of xor max can be 1, and if first bit of L is 1 and R is 1, first digit of max xor can be 0 coz in all the numbers in range L to R, first digit will be one in this case,
now we want max digits to differ for xor to be maximum, so we can see how many digits can differ and find accordingly
so what we do is, wherever we find the first differing digit between L and R, after that we can consider all positions of max xor to be 1, coz they can be diff. consider these examples:
Examples 1:
L = 8 R = 20
L ^ R = (01000) ^ (10100) = (11100)
Now as L ^ R is of form (1xxxx) we
can get maximum XOR as (11111) by
choosing A and B as 15 and 16 (01111
and 10000)
Examples 2:
L = 16 R = 20
L ^ R = (10000) ^ (10100) = (00100)
Now as L ^ R is of form (1xx) we can
get maximum xor as (111) by choosing
A and B as 19 and 20 (10011 and 10100)
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.