Print the largest integer formed by inserting digit X in given string
Last Updated :
20 Feb, 2023
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 = 9Output: 999Explanation: The largest number that can be formed is 999 after inserting 9 into “99”.
Input: S = “-13”, X = 2Output: -123Explanation: 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++
#include <bits/stdc++.h>
using namespace std;
string largestValue(string S, int X)
{
if (S[0] == '-') {
int f = 0;
for (int i = 0; i < S.length(); i++) {
char val = S[i];
if (i == 0) {
continue;
}
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);
}
}
else {
int f = 0;
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 S;
}
int main()
{
string S = "-13";
int X = 2;
cout << largestValue(S, X) << endl;
return 0;
}
Java
import java.io.*;
import java.util.*;
class GFG {
public static String largestValue(String S, int X)
{
if (S.charAt(0) == '-') {
int f = 0;
for (int i = 0; i < S.length(); i++) {
char val = S.charAt(i);
if (i == 0) {
continue;
}
if (X < Character.getNumericValue(val)) {
f = 1;
S = S.substring(0, i) + X
+ S.substring(i);
break;
}
}
if (f == 0) {
S = S + X;
}
}
else {
int f = 0;
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 S;
}
public static void main(String[] args)
{
String S = "-13";
int X = 2;
System.out.println(largestValue(S, X));
}
}
Python3
def largestValue(S, X):
if S[0] == '-':
f = 0
for i, val in enumerate(S):
if i == 0:
continue
if X < int(val):
f = 1
S = S[:i] + str(X) + S[i:]
break
if f == 0:
S = S + str(X)
else:
f = 0
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 S
S = "-13"
X = 2
print(largestValue(S, X))
Javascript
function largestValue(S, X) {
if (S[0] === '-') {
let f = 0;
for (let i = 0; i < S.length; i++) {
let val = S[i];
if (i === 0) {
continue;
}
if (X < parseInt(val)) {
f = 1;
S = S.slice(0, i) + X.toString() + S.slice(i);
break;
}
}
if (f === 0) {
S = S + X.toString();
}
}
else {
let f = 0;
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 S;
}
let S = "-13";
let X = 2;
console.log(largestValue(S, X));
C#
using System;
class GFG
{
static string LargestValue(string S, int X)
{
if (S[0] == '-') {
int f = 0;
for (int i = 0; i < S.Length; i++) {
char val = S[i];
if (i == 0) {
continue;
}
if (X < val - '0') {
f = 1;
S = S.Substring(0, i) + X.ToString()
+ S.Substring(i);
break;
}
}
if (f == 0) {
S = S + X.ToString();
}
}
else {
int f = 0;
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 S;
}
static void Main(string[] args)
{
string S = "-13";
int X = 2;
Console.WriteLine(LargestValue(S, X));
}
}
Time Complexity: O(N)Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...