Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | C ++ implementation of the above approach ; Function to check ; Converting integer to its equivalent binary number ; If adjacent character are same then increase counter ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void hasEqualBlockFrequency ( int N ) { string S = bitset < 3 > ( N ) . to_string ( ) ; set < int > p ; int c = 1 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) c += 1 ; else { p . insert ( c ) ; c = 1 ; } p . insert ( c ) ; } if ( p . size ( ) == 1 ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int N = 5 ; hasEqualBlockFrequency ( N ) ; return 0 ; }