XOR Profit sum problem

Sir basically my friend told solution for this problem but i am unable to crack the logic for this problem its clear that the maximum xor will be when the all bits are set but ideally would it be possible to obtain such combination in the given two pairs.

Thanks

A simple solution is to generate all pairs, find their XOR values and finally return the maximum XOR value.
An efficient solution is to consider pattern of binary values from L to R. 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. After observing the technique to get first bit, we can see that if we XOR L and R, the most significant bit of this XOR will tell us the maximum value we can achieve i.e. let XOR of L and R is 1xxx where x can be 0 or 1 then maximum XOR value we can get is 1111 because from L to R we have all possible combination of xxx and it is always possible to choose these bits in such a way from two numbers such that their XOR becomes all 1.

I hope it would clear all ur doubts…

1 Like

sir the first logic makes sense to me but when you said that there are all possible combination of xxx I couldn’t get it.

https://ide.codingblocks.com/s/622016 try to dry run code as it is not possible to explain the whole logic like this through chat…

Sir sorry to interrupt more meko sirf itna smaaj aya ki 1 st bit kis tarah ayegi uske baad kai reaming bits ke lia kya sochege samaj nahi ara if 1 st bit is one 1*** then the maximum vale you can tell is 1111but if its 0 we can be sure it can be *111 so how can i think for last n-1 bits.

just take 2-3 test cases and dry run on that

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.

1 Like