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
using System ; using System . Collections . Generic ; class GFG { static void hasEqualBlockFrequency ( int N ) { string S = Convert . ToString ( N , 2 ) ; HashSet < int > p = new HashSet < int > ( ) ; int c = 1 ; for ( int i = 0 ; i < S . Length - 1 ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) c += 1 ; else { p . Add ( c ) ; c = 1 ; } p . Add ( c ) ; } if ( p . Count == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static void Main ( ) { int N = 5 ; hasEqualBlockFrequency ( N ) ; } }