Form minimum number from given sequence | Javascript program to print minimum number that can be formed from a given sequence of Is and Ds ; min_avail represents the minimum number which is still available for inserting in the output vector . pos_of_I keeps track of the most recent index where ' I ' was encountered w . r . t the output vector ; vector to store the output ; cover the base cases ; Traverse rest of the input ; print the number ; Driver code
function printLeast ( arr ) { let min_avail = 1 , pos_of_I = 0 ; let al = [ ] ; if ( arr [ 0 ] == ' ' ) { al . push ( 1 ) ; al . push ( 2 ) ; min_avail = 3 ; pos_of_I = 1 ; } else { al . push ( 2 ) ; al . push ( 1 ) ; min_avail = 3 ; pos_of_I = 0 ; } for ( let i = 1 ; i < arr . length ; i ++ ) { if ( arr [ i ] == ' ' ) { al . push ( min_avail ) ; min_avail ++ ; pos_of_I = i + 1 ; } else { al . push ( al [ i ] ) ; for ( let j = pos_of_I ; j <= i ; j ++ ) al [ j ] = al [ j ] + 1 ; min_avail ++ ; } } for ( let i = 0 ; i < al . length ; i ++ ) document . write ( al [ i ] + " " ) ; document . write ( " " ) ; } printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ;