Print the largest integer formed by inserting digit X in given string
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.
Input: S = “99”, X = 9
The largest number that can be formed is 999 after inserting 9 into “99”.
Input: S = “-13”, X = 2
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:
- Else, if the number S is negative, then perform the following steps:
- Finally, print the largest possible string, represented by S.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)