Maximize the number N by inserting given digit at any position - GeeksforGeeks

05 Aug, 2021
Given a positive integer N and a digit K, the task is to find the maximum value of the given number N by inserting the given digit K in it N.
Examples:
Input: N = 6673, K = 6Output: 66763Explanation:All the numbers formed by inserting K at any position in N are {66673, 66763, 66736}. The maximum among all the formed number is 66763.
Input: N = 1234, K = 5Output: 51234
Approach: The given problem can be solved by inserting K at that position where the next digit is smaller than K. Follow the steps below to solve the problem:
Initialize two strings say S, by typecasting the given number N into a string and a string result as “” to store the maximum possible number after inserting K in it.
Initialize two variables, say L as the length of string S and i as 0. 
Traverse the string S until K is less than S[i] and append the character S[i] to the string result.
Now append K to the result and then append all the remaining characters of the string to the result.
After completing the above steps, print the string result and the maximum possible number.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the maximum value// of N after inserting the digit Kvoid maximizeNumber(int N, int K){    // Convert it into N to string    string s = to_string(N);    int L = s.length();     // Stores the maximum value of N    // after inserting K    string result;    int i = 0;     // Iterate till all digits that    // are not less than K    while ((i < L) && (K <= (s[i] - '0'))) {         // Add the current digit to        // the string result        result.push_back(s[i]);        ++i;    }     // Add digit 'K' to result    result.push_back(char(K + '0'));     // Iterate through all remaining    // characters    while (i < L) {         // Add current digit to result        result.push_back(s[i]);        ++i;    }     // Print the maximum number formed    cout << result;} // Driver Codeint main(){    int N = 6673, K = 6;    maximizeNumber(N, K);     return 0;}
// Java program for the above approach class GFG {     // Function to find the maximum value    // of N after inserting the digit Kpublic static void maximizeNumber(int N, int K){    // Convert it into N to string    String s = Integer.toString(N);    int L = s.length();     // Stores the maximum value of N    // after inserting K    String result =  "";    int i = 0;     // Iterate till all digits that    // are not less than K    while ((i < L) && (K <= ((int)s.charAt(i) - (int)'0'))) {         // Add the current digit to        // the string result        result += (s.charAt(i));        ++i;    }     // Add digit 'K' to result    result += ((char)(K + (int)'0'));     // Iterate through all remaining    // characters    while (i < L) {         // Add current digit to result        result += (s.charAt(i));        ++i;    }     // Print the maximum number formed    System.out.println(result);}     // Driver Code    public static void main (String args[]) {        int N = 6673, K = 6;        maximizeNumber(N, K);    } } // This code is contributed by _saurabh_Jaiswal.
# Python 3 program for the above approach # Function to find the maximum value# of N after inserting the digit Kdef maximizeNumber(N, K):     # Convert it into N to string    s = str(N)    L = len(s)     # Stores the maximum value of N    # after inserting K    result = ""    i = 0     # Iterate till all digits that    # are not less than K    while ((i < L) and (K <= (ord(s[i]) - ord('0')))):         # Add the current digit to        # the string result        result += (s[i])        i += 1     # Add digit 'K' to result    result += (chr(K + ord('0')))     # Iterate through all remaining    # characters    while (i < L):         # Add current digit to result        result += (s[i])        i += 1     # Print the maximum number formed    print(result)  # Driver Codeif __name__ == "__main__":     N = 6673    K = 6    maximizeNumber(N, K)     # This code is contributed by ukasp.
// C# program for above approachusing System; class GFG{     // Function to find the maximum value    // of N after inserting the digit Kpublic static void maximizeNumber(int N, int K){    // Convert it into N to string    String s = N.ToString();    int L = s.Length;     // Stores the maximum value of N    // after inserting K    string result =  "";    int i = 0;     // Iterate till all digits that    // are not less than K    while ((i < L) && (K <= ((int)s[i]- (int)'0'))) {         // Add the current digit to        // the string result        result += (s[i]);        ++i;    }     // Add digit 'K' to result    result += ((char)(K + (int)'0'));     // Iterate through all remaining    // characters    while (i < L) {         // Add current digit to result        result += (s[i]);        ++i;    }     // Print the maximum number formed     Console.Write(result);}// Driver Codestatic void Main(){         int N = 6673, K = 6;    maximizeNumber(N, K);}} // This code is contributed by sanjoy_62.
<script> // Javascript program for the above approach // Function to find the maximum value// of N after inserting the digit Kfunction maximizeNumber(N, K){     // Convert it into N to string    let s = String(N);    let L = s.length;     // Stores the maximum value of N    // after inserting K    let result = [];    let i = 0;     // Iterate till all digits that    // are not less than K    while ((i < L) && (K <= (s[i].charCodeAt(0) - '0'.charCodeAt(0)))) {         // Add the current digit to        // the string result        result.push(s[i]);        ++i;    }     // Add digit 'K' to result    result.push(String.fromCharCode(K + '0'.charCodeAt(0)));     // Iterate through all remaining    // characters    while (i < L) {         // Add current digit to result        result.push(s[i]);        ++i;    }     // Print the maximum number formed    document.write(result.join(""));} // Driver Codelet N = 6673, K = 6;maximizeNumber(N, K); // This code is contributed by gfgking.</script>
66763
 
Time Complexity: O(N)Auxiliary Space: O(N)
gfgking
ukasp
_saurabh_jaiswal
sanjoy_62
number-digits
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects