Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | Java 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
import java . util . * ; class GFG { static void hasEqualBlockFrequency ( int N ) { String S = Integer . toString ( N , 2 ) ; HashSet < Integer > p = new HashSet < Integer > ( ) ; int c = 1 ; for ( int i = 0 ; i < S . length ( ) - 1 ; i ++ ) { if ( S . charAt ( i ) == S . charAt ( i + 1 ) ) c += 1 ; else { p . add ( c ) ; c = 1 ; } p . add ( c ) ; } if ( p . size ( ) == 1 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 5 ; hasEqualBlockFrequency ( N ) ; } }