Check if bits of a number has count of consecutive set bits in increasing order | C ++ program to find if bit - pattern of a number has increasing value of continuous - 1 or not . ; Returns true if n has increasing count of continuous - 1 else false ; store the bit - pattern of n into bit bitset - bp ; set prev_count = 0 and curr_count = 0. ; increment current count of continuous - 1 ; traverse all continuous - 0 ; check prev_count and curr_count on encounter of first zero after continuous - 1 s ; check for last sequence of continuous - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findContinuous1 ( int n ) { const int bits = 8 * sizeof ( int ) ; string bp = bitset < bits > ( n ) . to_string ( ) ; int prev_count = 0 , curr_count = 0 ; int i = 0 ; while ( i < bits ) { if ( bp [ i ] == '1' ) { curr_count ++ ; i ++ ; } else if ( bp [ i - 1 ] == '0' ) { i ++ ; curr_count = 0 ; continue ; } else { if ( curr_count < prev_count ) return 0 ; i ++ ; prev_count = curr_count ; curr_count = 0 ; } } if ( prev_count > curr_count && ( curr_count != 0 ) ) return 0 ; return 1 ; } int main ( ) { int n = 179 ; if ( findContinuous1 ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }