Open In App

Print the largest integer formed by inserting digit X in given string

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S of size N representing a large integer value, and a positive digit X, the task is to print the largest integer formed by inserting the digit X in the string S.

Examples:

Input: S = “99”, X = 9
Output: 999
Explanation: 
The largest number that can be formed is 999 after inserting 9 into “99”.

Input: S = “-13”, X = 2
Output: -123
Explanation: 
The largest number that can be formed is -123 after inserting 2 into “-13”.

Approach: The problem can be solved by iterating over the character of the string S. Follow the steps below to solve this problem:

  • If the number S is positive, then perform the following steps:
    • Iterate in the range [0,  N-1] using the variable i and perform the following steps:
      • If S[i] is less than X then insert the digit X before S[i] and break the loop.
    • If none of the above cases satisfy, then append the digit X at the end of the string S.
  • Else, if the number S is negative, then perform the following steps:
    • Iterate in the range [0, N-1] in using the variable i and perform the following steps:
      • If S[i] is less than X, then insert the digit X before S[i] and break the loop.
    • If none of the above cases satisfy, then append the digit X at the end of the string S.
  • Finally, print the largest possible string, represented by S.

Below is the implementation of the above approach:

C++




// C++ code for the above JavaScript program
#include <bits/stdc++.h>
using namespace std;
 
// Function to find Largest Number after
// insertion of a digit
string largestValue(string S, int X)
{
  // If S is negative
  if (S[0] == '-') {
    int f = 0;
    // Iterate through characters of S
    for (int i = 0; i < S.length(); i++) {
      char val = S[i];
      if (i == 0) {
        continue;
      }
      // If digit x is less
      // than S[i] insert digit
      // after X
      if (X < val - '0') {
        f = 1;
        S = S.substr(0, i) + to_string(X)
          + S.substr(i);
        break;
      }
    }
    if (f == 0) {
      S = S + to_string(X);
    }
  }
  // If S is positive
  else {
    int f = 0;
    // If x > S[i] insert x
    for (int i = 0; i < S.length(); i++) {
      char val = S[i];
      if (X > val - '0') {
        f = 1;
        S = S.substr(0, i) + to_string(X)
          + S.substr(i);
        break;
      }
    }
    if (f == 0) {
      S = S + to_string(X);
    }
  }
  // Return the answer
  return S;
}
 
// Driver Code
int main()
{
  // Given Input
  string S = "-13";
  int X = 2;
  // Function Call
  cout << largestValue(S, X) << endl;
  return 0;
}
// This code is contributed by phasing17.


Java




// Java code for the above approach
 
import java.io.*;
import java.util.*;
class GFG {
 
    // Function to find Largest Number after
    // insertion of a digit
    public static String largestValue(String S, int X)
    {
        // If S is negative
        if (S.charAt(0) == '-') {
            int f = 0;
            // Iterate through characters of S
            for (int i = 0; i < S.length(); i++) {
                char val = S.charAt(i);
                if (i == 0) {
                    continue;
                }
 
                // If digit X is less
                // than S[i] insert digit
                // after X
                if (X < Character.getNumericValue(val)) {
                    f = 1;
                    S = S.substring(0, i) + X
                        + S.substring(i);
                    break;
                }
            }
            if (f == 0) {
                S = S + X;
            }
        }
        // If S is positive
        else {
            int f = 0;
 
            // If X > S[i] insert X
            for (int i = 0; i < S.length(); i++) {
                char val = S.charAt(i);
                if (X > Character.getNumericValue(val)) {
                    f = 1;
                    S = S.substring(0, i) + X
                        + S.substring(i);
                    break;
                }
            }
            if (f == 0) {
                S = S + X;
            }
        }
        // Return the answer
        return S;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Input
        String S = "-13";
        int X = 2;
 
        // Function Call
        System.out.println(largestValue(S, X));
    }
}


Python3




# Python program for the above approach
 
# Function to find Largest Number after
# insertion of a digit
def largestValue(S, X):
 
    # If S is negative
    if S[0] == '-':
        f = 0
        # Iterate through characters of S
        for i, val in enumerate(S):
            if i == 0:
                continue
 
            # If digit x is less
            # than S[i] insert digit
            # after X
            if X < int(val):
                f = 1
                S = S[:i] + str(X) + S[i:]
                break
 
        if f == 0:
            S = S + str(X)
 
    # If S is positive
    else:
        f = 0
 
        # If x > S[i] insert x
        for i, val in enumerate(S):
            if X > int(val):
                f = 1
                S = S[:i] + str(X) + S[i:]
                break
 
        if f == 0:
            S = S + str(X)
 
    # Return the answer
    return S
 
 
# Driver Code
 
# Given Input
S = "-13"
X = 2
 
# Function Call
print(largestValue(S, X))


Javascript




// JavaScript program for the above approach
 
// Function to find Largest Number after
// insertion of a digit
function largestValue(S, X) {
 
  // If S is negative
  if (S[0] === '-') {
    let f = 0;
     
    // Iterate through characters of S
    for (let i = 0; i < S.length; i++) {
      let val = S[i];
      if (i === 0) {
        continue;
      }
 
      // If digit x is less
      // than S[i] insert digit
      // after X
      if (X < parseInt(val)) {
        f = 1;
        S = S.slice(0, i) + X.toString() + S.slice(i);
        break;
      }
    }
 
    if (f === 0) {
      S = S + X.toString();
    }
  }
 
  // If S is positive
  else {
    let f = 0;
 
    // If x > S[i] insert x
    for (let i = 0; i < S.length; i++) {
      let val = S[i];
      if (X > parseInt(val)) {
        f = 1;
        S = S.slice(0, i) + X.toString() + S.slice(i);
        break;
      }
    }
 
    if (f === 0) {
      S = S + X.toString();
    }
  }
 
  // Return the answer
  return S;
}
 
// Driver Code
 
// Given Input
let S = "-13";
let X = 2;
 
// Function Call
console.log(largestValue(S, X));
 
// This code is contributed by phasing17.


C#




// C# code for the above approach
using System;
 
class GFG
{
 
  // Function to find Largest Number after
  // insertion of a digit
  static string LargestValue(string S, int X)
  {
 
    // If S is negative
    if (S[0] == '-') {
      int f = 0;
 
      // Iterate through characters of S
      for (int i = 0; i < S.Length; i++) {
        char val = S[i];
        if (i == 0) {
          continue;
        }
 
        // If digit x is less
        // than S[i] insert digit
        // after X
        if (X < val - '0') {
          f = 1;
          S = S.Substring(0, i) + X.ToString()
            + S.Substring(i);
          break;
        }
      }
      if (f == 0) {
        S = S + X.ToString();
      }
    }
 
    // If S is positive
    else {
      int f = 0;
 
      // If x > S[i] insert x
      for (int i = 0; i < S.Length; i++) {
        char val = S[i];
        if (X > val - '0') {
          f = 1;
          S = S.Substring(0, i) + X.ToString()
            + S.Substring(i);
          break;
        }
      }
      if (f == 0) {
        S = S + X.ToString();
      }
    }
 
    // Return the answer
    return S;
  }
 
  // Driver code
  static void Main(string[] args)
  {
 
    // Given Input
    string S = "-13";
    int X = 2;
 
    // Function Call
    Console.WriteLine(LargestValue(S, X));
  }
}
 
// This code is contributed by phasing17.


Output

-123

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads