Maximize the given number by replacing a segment of digits with the alternate digits given
Last Updated :
06 Feb, 2023
Given many N digits. We are also given 10 numbers which represents the alternate number for all the one-digit numbers from 0 to 9. We can replace any digit in N with the given alternate digit to it, but we are only allowed to replace any consecutive segment of numbers for once only, the task is to replace any consecutive segment of numbers such that the obtained number is the largest of all possible replacements.
Examples:
Input: n = 1337, a[] = {0, 1, 2, 5, 4, 6, 6, 3, 1, 9}
Output: 1557
1 can be replaced with 1 as a[1] = 1 (No effect)
3 can be replaced with 5
7 can be replaced with 3 (isn’t required if the number needs to be maximized)
Input: number = 11111, a[] = {0, 5, 2, 5, 4, 6, 6, 3, 1, 9}
Output: 55555
Approach: Since we need to get the largest number possible, hence iterate from the left and find the number whose alternate number is greater than the current one, and keep replacing the upcoming numbers till the alternate number is not smaller.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string get_maximum(string s, int a[])
{
int n = s.size();
for ( int i = 0; i < n; i++) {
if (s[i] - '0' < a[s[i] - '0' ]) {
int j = i;
while (j < n && (s[j] - '0' <= a[s[j] - '0' ])) {
s[j] = '0' + a[s[j] - '0' ];
j++;
}
return s;
}
}
return s;
}
int main()
{
string s = "1337" ;
int a[] = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 };
cout << get_maximum(s, a);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static String get_maximum( char [] s, int a[])
{
int n = s.length;
for ( int i = 0 ; i < n; i++)
{
if (s[i] - '0' < a[s[i] - '0' ])
{
int j = i;
while (j < n && (s[j] - '0' <= a[s[j] - '0' ]))
{
s[j] = ( char ) ( '0' + a[s[j] - '0' ]);
j++;
}
return String.valueOf(s);
}
}
return String.valueOf(s);
}
public static void main(String[] args)
{
String s = "1337" ;
int a[] = { 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 };
System.out.println(get_maximum(s.toCharArray(), a));
}
}
|
Python3
def get_maximum(s, a) :
s = list (s)
n = len (s)
for i in range (n) :
if ( ord (s[i]) - ord ( '0' ) < a[ ord (s[i]) - ord ( '0' )]) :
j = i
while (j < n and ( ord (s[j]) - ord ( '0' ) < =
a[ ord (s[j]) - ord ( '0' )])) :
s[j] = chr ( ord ( '0' ) + a[ ord (s[j]) - ord ( '0' )])
j + = 1
return "".join(s);
return s
if __name__ = = "__main__" :
s = "1337"
a = [ 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 ]
print (get_maximum(s, a))
|
C#
using System;
class GFG
{
static String get_maximum( char [] s, int [] a)
{
int n = s.Length;
for ( int i = 0; i < n; i++)
{
if (s[i] - '0' < a[s[i] - '0' ])
{
int j = i;
while (j < n && (s[j] - '0' <= a[s[j] - '0' ]))
{
s[j] = ( char ) ( '0' + a[s[j] - '0' ]);
j++;
}
return String.Join( "" ,s);
}
}
return String.Join( "" ,s);
}
public static void Main(String[] args)
{
String s = "1337" ;
int [] a = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 };
Console.WriteLine(get_maximum(s.ToCharArray(), a));
}
}
|
PHP
<?php
function get_maximum( $s , $a )
{
$n = strlen ( $s );
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s [ $i ] - '0' < $a [ $s [ $i ] - '0' ])
{
$j = $i ;
while ( $j < $n && ( $s [ $j ] - '0' <= $a [ $s [ $j ] - '0' ]))
{
$s [ $j ] = '0' + $a [ $s [ $j ] - '0' ];
$j ++;
}
return $s ;
}
}
return $s ;
}
$s = "1337" ;
$a = array ( 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 );
echo get_maximum( $s , $a );
?>
|
Javascript
function getMaximum(s, a) {
let n = s.length;
for (let i = 0; i < n; i++) {
if (parseInt(s[i]) < a[parseInt(s[i])]) {
let j = i;
while (j < n && (parseInt(s[j]) <= a[parseInt(s[j])])) {
s = s.slice(0, j) + a[parseInt(s[j])] + s.slice(j + 1);
j++;
}
return s;
}
}
return s;
}
console.log(getMaximum( "1337" , [0, 1, 2, 5, 4, 6, 6, 3, 1, 9]));
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...