Find the largest number that can be formed by changing at most K digits | Function to return the maximum number that can be formed by changing at most k digits in str ; For every digit of the number ; If no more digits can be replaced ; If current digit is not already 9 ; Replace it with 9 ; One digit has been used ; Driver code
function findMaximumNum ( str , n , k ) { for ( var i = 0 ; i < n ; i ++ ) { if ( k < 1 ) break ; if ( str [ i ] !== " " ) { str [ i ] = " " ; k -- ; } } return str . join ( " " ) ; } var str = " " ; var n = str . length ; var k = 3 ; document . write ( findMaximumNum ( str . split ( " " ) , n , k ) ) ;