Given an integer n > 0, the task is to find whether this integer has an alternate pattern in its bits representation. For example- 5 has an alternate pattern i.e. 101.
Print “Yes” if it has an alternate pattern otherwise “No”. Here alternate pattern can be like 0101 or 1010.
Input : 15 Output : No Explanation: Binary representation of 15 is 1111. Input : 10 Output : Yes Explanation: Binary representation of 10 is 1010.
A simple approach is to find its binary equivalent and then check its bits.
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Check whether bits are in alternate pattern in the given range
- Check whether bits are in alternate pattern in the given range | Set-2
- Print numbers in the range 1 to n having bits in alternate pattern
- Alternate bits of two numbers to create a new number
- Check if bits of a number has count of consecutive set bits in increasing order
- Check whether the number has only first and last bits set | Set 2
- Check if all bits of a number are set
- Check whether the number has only first and last bits set
- Check if a number has two adjacent set bits
- Check if a number has same number of set and unset bits
- Check if all bits can be made same by flipping two consecutive bits
- Toggle bits of a number except first and last bits
- Check whether all the bits are set in the given range
- Check whether all the bits are unset in the given range
Improved By : nitin mittal