Maximize the number N by inserting given digit at any position | Java program for the above approach ; 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
class GFG { public static void maximizeNumber ( int N , int K ) { String s = Integer . toString ( N ) ; int L = s . length ( ) ; String result = " " ; int i = 0 ; while ( ( i < L ) && ( K <= ( ( int ) s . charAt ( i ) - ( int ) '0' ) ) ) { result += ( s . charAt ( i ) ) ; ++ i ; } result += ( ( char ) ( K + ( int ) '0' ) ) ; while ( i < L ) { result += ( s . charAt ( i ) ) ; ++ i ; } System . out . println ( result ) ; } public static void main ( String args [ ] ) { int N = 6673 , K = 6 ; maximizeNumber ( N , K ) ; } }