![]() |
VOOZH | about |
Given two positive integers x and y (0 < x, y < 2^32), check if one integer is obtained by rotating bits of the other.
Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end.
Examples:
Input : a = 8, b = 1
Output : yes
Explanation : Representation of a = 8 : 0000 0000 0000 0000 0000 0000 0000 1000 ,Representation of b = 1 : 0000 0000 0000, 0000 0000 0000 0000 0001. If we rotate a by 3 units right we get b, hence answer is yes.Input : a = 122, b = 2147483678
Output : yes
Explanation :Representation of a = 122 : 0000 0000 0000 0000 0000 0000 0111 1010,Representation of b = 2147483678 : 1000 0000 0000 0000 0000 0000 0001 1110, If we rotate a by 2 units right we get b, hence answer is yes.
Approach:
Here is implementation of above algorithm.
yes
Time Complexity: O(logn)
Auxiliary Space: O(1)