Find a value whose XOR with a given value is maximum.
You are given an integer 'X' and your task is to find an integer 'Y' such that the bitwise XOR of the integers 'X' and 'Y' give the maximum possible value. The integer 'Y' should not be greater than 2305843009213693951 ((2^61) - 1).
A bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only one of the bits is 1, but will be 0 if both are 0 or both are 1.
Note:
1. The maximum obtainable value can always be stored in 64-bit memory space.
2. The given number 'X' is always non-negative.
Input Format:
The first line of the input contains an integer 'T' denoting the number of test cases.
The first line and the only line of each test case contains an integer 'X'.
Output Format:
For each test case, print an integer 'Y' whose XOR with 'X' yields the maximum obtainable value in separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10^4
0 <= X <= 10^18
Time Limit: 1sec
CodingNinjas
author
2y
Step 1 : Sort the array.
Step 2 : Xor all elements of the array.
Step 3 : Remove maximum element from xor by doing xor_array ^= current_max_element.
Step 4 : return the resultant array.
CodingNinjas
author
2y
Bits Manipulation and properties of bitwise XOR
- Consider two numbers ‘A’ = 7, ‘B’ = 3. Bitwise representation of ‘A’ = 0 1 1 1, and ‘B’ = 0 0 1 1, as you can see, there is an extra bit set in ‘A’ as co...read more
Help your peers!
Add answer anonymously...
Top Urban Company Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Urban Company Software Developer
>
Urban Company Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app