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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 | Set-2
- Check if bits of a number has count of consecutive set bits in increasing order
- Check whether bits are in alternate pattern in the given range
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Print numbers in the range 1 to n having bits in alternate pattern
- Check whether the number has only first and last bits set | Set 2
- Check if a number has same number of set and unset bits
- Check if a number has two adjacent set bits
- Check whether the number has only first and last bits set
- Alternate bits of two numbers to create a new number
- Check if all bits can be made same by flipping two consecutive bits
- Count pairs in an array such that both elements has equal set bits
- Print numbers having first and last bits as the only set bits
- Toggle bits of a number except first and last bits
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Check if all bits of a number are set
- Prime Number of Set Bits in Binary Representation | Set 1
- Next higher number with same number of set bits
- M-th smallest number having k number of set bits.
Improved By : nitin mittal