Maximize the number N by inserting given digit at any position
Last Updated :
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 = 6
Output: 66763
Explanation:
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 = 5
Output: 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++
#include <bits/stdc++.h>
using namespace std;
void maximizeNumber( int N, int K)
{
string s = to_string(N);
int L = s.length();
string result;
int i = 0;
while ((i < L) && (K <= (s[i] - '0' ))) {
result.push_back(s[i]);
++i;
}
result.push_back( char (K + '0' ));
while (i < L) {
result.push_back(s[i]);
++i;
}
cout << result;
}
int main()
{
int N = 6673, K = 6;
maximizeNumber(N, K);
return 0;
}
|
Java
class GFG {
public static void maximizeNumber( int N, int K)
{
String s = Integer.toString(N);
int L = s.length();
String result = "" ;
int i = 0 ;
while ((i < L) && (K <= (( int )s.charAt(i) - ( int ) '0' ))) {
result += (s.charAt(i));
++i;
}
result += (( char )(K + ( int ) '0' ));
while (i < L) {
result += (s.charAt(i));
++i;
}
System.out.println(result);
}
public static void main (String args[]) {
int N = 6673 , K = 6 ;
maximizeNumber(N, K);
}
}
|
Python3
def maximizeNumber(N, K):
s = str (N)
L = len (s)
result = ""
i = 0
while ((i < L) and (K < = ( ord (s[i]) - ord ( '0' )))):
result + = (s[i])
i + = 1
result + = ( chr (K + ord ( '0' )))
while (i < L):
result + = (s[i])
i + = 1
print (result)
if __name__ = = "__main__" :
N = 6673
K = 6
maximizeNumber(N, K)
|
C#
using System;
class GFG{
public static void maximizeNumber( int N, int K)
{
String s = N.ToString();
int L = s.Length;
string result = "" ;
int i = 0;
while ((i < L) && (K <= (( int )s[i]- ( int ) '0' ))) {
result += (s[i]);
++i;
}
result += (( char )(K + ( int ) '0' ));
while (i < L) {
result += (s[i]);
++i;
}
Console.Write(result);
}
static void Main()
{
int N = 6673, K = 6;
maximizeNumber(N, K);
}
}
|
Javascript
<script>
function maximizeNumber(N, K)
{
let s = String(N);
let L = s.length;
let result = [];
let i = 0;
while ((i < L) && (K <= (s[i].charCodeAt(0) - '0' .charCodeAt(0)))) {
result.push(s[i]);
++i;
}
result.push(String.fromCharCode(K + '0' .charCodeAt(0)));
while (i < L) {
result.push(s[i]);
++i;
}
document.write(result.join( "" ));
}
let N = 6673, K = 6;
maximizeNumber(N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...