Find the maximum possible Binary Number from given string | Function to return maximum number that can be formed from the string ; To store the frequency of ' z ' and ' n ' in the given string ; Number of zeroes ; Number of ones ; To store the required number ; Add all the ones ; Add all the zeroes ; Driver code
function maxNumber ( str , n ) { var freq = Array ( 2 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { freq [ 0 ] ++ ; } else if ( str [ i ] == ' ' ) { freq [ 1 ] ++ ; } } var num = " " ; for ( var i = 0 ; i < freq [ 1 ] ; i ++ ) num += ' ' ; for ( var i = 0 ; i < freq [ 0 ] ; i ++ ) num += ' ' ; return num ; } var str = " " ; var n = str . length ; document . write ( maxNumber ( str , n ) ) ;