Minimize the number by changing at most K digits | Function to return the minimized number ; Total digits in the number ; If the string is empty or there are no operations to perform ; "0" is a valid number ; If the first digit is not already 1 then update it to 1 and decrement k ; While there are operations left and the number can still be updated ; If the current digit is not already 0 then update it to 0 and decrement k ; Return the minimised number ; Driver code
function minNum ( num , k ) { let len = num . length ; if ( len == 0 k == 0 ) { let num_str = num . join ( " " ) ; return num_str ; } if ( len == 1 ) return " " ; if ( num [ 0 ] != ' ' ) { num [ 0 ] = ' ' ; k -- ; } let i = 1 ; while ( k > 0 && i < len ) { if ( num [ i ] != ' ' ) { num [ i ] = ' ' ; k -- ; } i ++ ; } let num_str = num . join ( " " ) ; return num_str ; } let num = " " ; let k = 3 ; document . write ( minNum ( num . split ( " " ) , k ) ) ;