Maximize the number N by inserting given digit at any position | Function to find the maximum value of N after inserting the digit K ; Convert it into N to string ; Stores the maximum value of N after inserting K ; Iterate till all digits that are not less than K ; Add the current digit to the string result ; Add digit ' K ' to result ; Iterate through all remaining characters ; Add current digit to result ; Print the maximum number formed ; Driver Code
function maximizeNumber ( N , K ) { let s = String ( N ) ; let L = s . length ; let result = [ ] ; let i = 0 ; while ( ( i < L ) && ( K <= ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ) { result . push ( s [ i ] ) ; ++ i ; } result . push ( String . fromCharCode ( K + ' ' . charCodeAt ( 0 ) ) ) ; while ( i < L ) { result . push ( s [ i ] ) ; ++ i ; } document . write ( result . join ( " " ) ) ; } let N = 6673 , K = 6 ; maximizeNumber ( N , K ) ;