I used the brute force approach .Icould not understand the optimized way given in the solution
Couldnt understand the given solution to this problem
hey, you can observe that if binary form of L and R have same digit then first bit of both the number will be same like in range (5,6)(101,110) both have 3 binary digit so first bit remains same but if r have more binary digit than l then first bit will change from 0 to 1 (5,9)(0110,1001) so we xor l and r and find first bit from left in the xored value ,it means only the values which are right side of this bit must have been shifted from 0 to 1 or 1 to 0 bcoz left bits becomes zero during xor means they must be same for entire numbers in that range ,so you can conclude that all the bit in right side of that first bit become 1 in atleast one case and that will be your maximum
i Didnt understand the given solution
you can simply observe it as when you go from a low number to high number bit changes from right to left and leftmost bits are only affected when your number is getting bigger since then you require high powers of 2.so here we are going from L to R when we xored L and R and find the leftmost bit(1st set bit while going left to right).the bits which are left of this bits are 0(since we have find the leftmost set bit) this means this left bits never changes in the whole range from L to R.means only bits which are on the right side of this leftmost set bit changes we know XOR becomes 1 only when the bits are different since right bits from its left most bits are changing so there must exist a pair by xoring which we can make all bits from this leftmost set bit the rightest position all set.and that will be you answer(means just find leftmost set bit by xoring L and R and make all bits including this which are in the right side of this set and it will become your answer).
Hope it helps,
if still you have doubt you can ask me else mark this doubt as resolved. Thankyou
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.