![]() |
VOOZH | about |
Given a positive integer n. The problem is to check whether this integer has an alternate pattern in its binary representation or not. Here alternate pattern means that the set and unset bits in n occur in alternate order. For example- 5 has an alternate pattern i.e. 101.
Print βYesβ if it has an alternate pattern otherwise βNoβ.
Note: 0 < n.
Examples :
Input : 10 Output : Yes (10)10 = (1010)2, has an alternate pattern. Input : 12 Output : No (12)10 = (1100)2, does not have an alternate pattern.
Simple Approach: It has been discussed in this post having a time complexity of O(n).
Efficient Approach: Following are the steps:
Output :
Yes
Time Complexity : O(1)
Auxiliary Space : O(1)