
42 is valid because it's binary representation '101010' has no two adjacent bits that are the same.
The first line contains a single integer T representing the number of test cases.
The first line of each test case will contain a single integer ‘N’ which denotes the given number.
For each test case, return true if no two adjacent bits are the same at the right of the leftmost set bit otherwise return false.
You don’t need to print anything, it has already been taken care of. Just implement the given function.
Can you solve this using O(1) time complexity?
1 <= T <= 10
1 <= N <= 10^9
Time Limit: 1 sec
The idea is to first find the leftmost set bit and then check all the bits on the right of that, if the adjacent bits are the same or not.
Here is the algorithm:
The idea is to use XOR operator in such a way that if all the adjacent bits are different, then all the bits of the new number will be set to true, this is done so that the problem gets modified into: “check if all bits are set”.
Below is the algorithm: