

The first line contains a single integer ‘T’ representing the number of test cases.Then test cases follows :
The first line of each test case contains an integer ‘N’.
For each test case, print the value of ‘K’ or -1 if no such ‘K’ exists.
Print the output of each test case in a new line.
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10^9
Time Limit : 1 sec
We will first assume that the value of K > N is valid.
So two cases arise here :
In this case, we can always find some smaller positive integer as the leftmost bits are just added to small integers to make larger numbers. Since , if bits in both are same , their XOR will be 0.
E.g. Let K = 6 , then ( K - 1 ) = 5.
In binary representation, K = 110 and ( K - 1 ) = 101
Then K XOR ( K - 1 ) = 011, that is 3.
The same value can be received by taking K = 2, ( K - 1 ) = 1.
K XOR ( K - 1 ) = 011 , that is 3.
2. All the bits left to N in K and K-1 are not the same.
In this case, their XOR will be greater than K so it will never be equal to N.
Hence, we just need to check for K <= N if some valid integer is found such that K XOR ( K - 1 ) = N .
Algorithm :
Approach :
Last bit of (K - 1) is 0 and the last bit of ‘K’ is 1. Rest of the bits are the same in both the numbers. So XOR would always be 1 if ( K-1 ) is even.
Last bit in ( K - 1 ) is 1. And in K, the last bit is 0.
But in this case there may be more bits which differ due to carry. The carry continues to propagate to the left till we find the first 0 bit. So (K) XOR ( K - 1) will be 2^i-1 where i is the position of the first 0 bit in ‘K’ from the left.
So, we can say that if N is of form 2^i-1 then we will have our answer as N/2+1.
Algorithm :