Minimum number of distinct powers of 2 required to express a given binary number | Function to return the minimum distinct powers of 2 required to express s ; Reverse the string to start from lower powers ; Check if the character is 1 ; Add in range provided range ; Initialize the counter ; Check if the character is not 0 ; Increment the counter ; Print the result ; Driver Code
function findMinimum ( s ) { var n = s . length ; var x = Array ( n + 1 ) . fill ( 0 ) ; s . reverse ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { if ( x [ i ] == 1 ) { x [ i + 1 ] = 1 ; x [ i ] = 0 ; } else if ( i && x [ i - 1 ] == 1 ) { x [ i + 1 ] = 1 ; x [ i - 1 ] = - 1 ; } else x [ i ] = 1 ; } } var c = 0 ; for ( var i = 0 ; i <= n ; i ++ ) { if ( x [ i ] != 0 ) c ++ ; } document . write ( c ) ; } var str = " " . split ( ' ' ) ; findMinimum ( str ) ;