Given a integer n > 0, the task is to find whether in the bit pattern of integer count of continuous 1’s are in increasing from left to right.
Input:19 Output:Yes Explanation: Bit-pattern of 19 = 10011, Counts of continuous 1's from left to right are 1, 2 which are in increasing order. Input : 183 Output : yes Explanation: Bit-pattern of 183 = 10110111, Counts of continuous 1's from left to right are 1, 2, 3 which are in increasing order.
A simple solution is to store binary representation of given number into a string, then traverse from left to right and count the number of continuous 1’s. For every encounter of 0 check the value of previous count of continuous 1’s to that of current value, if the value of previous count is greater than the value of current count then return False, Else when string ends return True.
An efficient solution is to use decimal to binary conversion loop that divides number by 2 and take remainder as bit. This loop finds bits from right to left. So we check if right to left is in decreasing order or not.
Below is the implementation.
This article is contributed by Shivam Pradhan (anuj_charm). 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.
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- 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 all bits can be made same by flipping two consecutive bits
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has two adjacent set bits
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Check whether the number has only first and last bits set
- Count pairs in an array such that both elements has equal set bits
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Count number of binary strings without consecutive 1’s : Set 2
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Print numbers having first and last bits as the only set bits
- Toggle bits of a number except first and last bits
- Sort an array according to count of set bits | Set 2
- Count total set bits in all numbers from 1 to n | Set 2
- Count total set bits in all numbers from 1 to N | Set 3
- Check if all bits of a number are set