Find the possible permutation of the bits of N | Javascript implementation of the approach ; Function that returns true if it is possible to arrange the bits of n in alternate fashion ; To store the count of 1 s in the binary representation of n ; If the number set bits and the number of unset bits is equal ; Driver code
const TOTAL_BITS = 32 ; function CountBits ( value ) { let count = 0 ; while ( value != 0 ) { count ++ ; value &= value - 1 ; } return count ; } function isPossible ( n ) { let cnt = CountBits ( n ) ; if ( cnt == parseInt ( TOTAL_BITS / 2 ) ) return true ; return false ; } let n = 524280 ; if ( isPossible ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;