Find smallest number formed by inserting given digit
Last Updated :
23 Jul, 2021
Given a string N and a digit X ([1, 9]), the task is to find the minimum integer number formed by inserting digit X anywhere in N.
Examples:
Input: N = “89”, X = 1
Output: “189″
Explanation: X can be inserted at 3 positions {189, 891, 819} and 189 is the minimum.
Input: N = “-12”, X = 3
Output: “-312″
Naive Approach: A simple approach to this problem is to insert X in all the positions (except the left of the negative sign if present) and find the minimum among all the numbers formed.The approach is inefficient in the case of larger strings.
Efficient Approach: The main idea is that if N is a positive insert in such a way that the number formed is minimum whereas if N is negative, then insert in X such as the number formed is maximum, ignoring the negative sign. Follow the steps below to optimize the above approach:
- Initialize two variables, say len = length of string N and position = n + 1.
- If N is negative (N[0] = ‘-‘), traverse the string from (n-1)th index to 1th index and check if N[i] – ‘0’ < X, if true then update position = i.
- If N is positive, traverse the string from (n-1)th index to 0th index and check if N[i] – ‘0’ > X, if true then update position = i.
- Insert X at index position in N.
- Finally, return the string N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string MinValue(string N, int X)
{
int len = N.size();
int position = len + 1;
if (N[0] == '-' ) {
for ( int i = len - 1; i >= 1; i--) {
if ((N[i] - '0' ) < X) {
position = i;
}
}
}
else {
for ( int i = len - 1; i >= 0; i--) {
if ((N[i] - '0' ) > X) {
position = i;
}
}
}
N.insert(N.begin() + position, X + '0' );
return N;
}
int main()
{
string N = "89" ;
int X = 1;
cout << MinValue(N, X) << "\n" ;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static String MinValue(String number, int x)
{
int length = number.length();
int position = length + 1 ;
if (number.charAt( 0 ) == '-' ) {
for ( int i = number.length() - 1 ; i >= 1 ; --i) {
if ((number.charAt(i) - 48 ) < x) {
position = i;
}
}
}
else {
for ( int i = number.length() - 1 ; i >= 0 ; --i) {
if ((number.charAt(i) - 48 ) > x) {
position = i;
}
}
}
number
= number.substring( 0 , position) + x
+ number.substring(position, number.length());
return number.toString();
}
public static void main(String[] args)
{
String number = "89" ;
int x = 1 ;
System.out.print(MinValue(number, x));
}
}
|
Python3
def MinValue(N, X):
N = list (N);
ln = len (N)
position = ln + 1
if (N[ 0 ] = = '-' ):
for i in range (ln - 1 , 0 , - 1 ):
if (( ord (N[i]) - ord ( '0' )) < X):
position = i
else :
for i in range (ln - 1 , - 1 , - 1 ):
if (( ord (N[i]) - ord ( '0' )) > X):
position = i
c = chr (X + ord ( '0' ))
str = N.insert(position, c);
return ''.join(N)
N = "89"
X = 1
print (MinValue(N, X))
|
C#
using System;
class GFG {
static String MinValue( string number, int x)
{
int length = number.Length;
int position = length + 1;
if (number[0] == '-' ) {
for ( int i = number.Length - 1; i >= 1; --i) {
if ((number[i] - 48) < x) {
position = i;
}
}
}
else {
for ( int i = number.Length - 1; i >= 0; --i) {
if ((number[i] - 48) > x) {
position = i;
}
}
}
number
= number.Substring(0, position) + x
+ number.Substring(position, number.Length);
return number.ToString();
}
public static void Main()
{
string number = "89" ;
int x = 1;
Console.WriteLine(MinValue(number, x));
}
}
|
Javascript
<script>
function MinValue(N, X) {
let len = N.length;
let position = len + 1;
if (N[0] == '-' ) {
for (let i = len - 1; i >= 1; i--) {
if ((N[i].charCodeAt(0) - '0' .charCodeAt(0)) < X) {
position = i;
}
}
}
else {
for (let i = len - 1; i >= 0; i--) {
if ((N[i].charCodeAt(0) - '0' .charCodeAt(0)) > X) {
position = i;
}
}
}
const c = String.fromCharCode(X + '0' .charCodeAt(0));
let str = N.slice(0, position) + c + N.slice(position);
return str;
}
let N = "89" ;
let X = 1;
document.write(MinValue(N, X));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...