Maximize the value of A by replacing some of its digits with digits of B
Last Updated :
15 Sep, 2022
Given two string A and B which represents two integers, the task is to print the maximized value of A after replacing 0 or more digits of A with any digit of B.
Note: A digit in B can only be used once.
Examples:
Input: A = “1234”, B = “4321”
Output: 4334
1 can be replaced with 4 and 2 can be replaced with 3.
Input: A = “1002”, B = “100”
Output: 1102
The first 0 can be replaced with a 1.
Approach: Since the value of A has to maximized, any digit will be replaced by only digits of greater value. The digits on the left have more significance in contributing to the value, so they should be replaced with as large values as possible. Sort B and iterate from left to right in A and try replacing the current digit with the maximum of available options if possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string maxValue(string a, string b)
{
sort(b.begin(), b.end());
int n = a.length();
int m = b.length();
int j = m - 1;
for ( int i = 0; i < n; i++) {
if (j < 0)
break ;
if (b[j] > a[i]) {
a[i] = b[j];
j--;
}
}
return a;
}
int main()
{
string a = "1234" ;
string b = "4321" ;
cout << maxValue(a, b);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String maxValue( char []a, char []b)
{
Arrays.sort(b);
int n = a.length;
int m = b.length;
int j = m - 1 ;
for ( int i = 0 ; i < n; i++) {
if (j < 0 )
break ;
if (b[j] > a[i]) {
a[i] = b[j];
j--;
}
}
return String.valueOf(a);
}
public static void main(String[] args)
{
String a = "1234" ;
String b = "4321" ;
System.out.print(maxValue(a.toCharArray(), b.toCharArray()));
}
}
|
Python3
def maxValue(a, b):
b = sorted (b)
bi = [i for i in b]
ai = [i for i in a]
n = len (a)
m = len (b)
j = m - 1
for i in range (n):
if (j < 0 ):
break
if (bi[j] > ai[i]):
ai[i] = bi[j]
j - = 1
x = "" . join(ai)
return x
a = "1234"
b = "4321"
print (maxValue(a, b))
|
C#
using System;
class GFG
{
static String maxValue( char []a, char []b)
{
Array.Sort(b);
int n = a.Length;
int m = b.Length;
int j = m - 1;
for ( int i = 0; i < n; i++)
{
if (j < 0)
break ;
if (b[j] > a[i])
{
a[i] = b[j];
j--;
}
}
return String.Join( "" ,a);
}
public static void Main(String[] args)
{
String a = "1234" ;
String b = "4321" ;
Console.Write(maxValue(a.ToCharArray(), b.ToCharArray()));
}
}
|
PHP
<?php
function maxValue( $a , $b )
{
sort( $b );
$n = sizeof( $a );
$m = sizeof( $b );
$j = $m - 1;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $j < 0)
break ;
if ( $b [ $j ] > $a [ $i ])
{
$a [ $i ] = $b [ $j ];
$j --;
}
}
$a = implode( "" , $a );
return $a ;
}
# convert string into array
$a = str_split ( "1234" );
$b = str_split ( "4321" );
echo maxValue( $a , $b );
?>
|
Javascript
<script>
function maxValue(a,b)
{
b.sort( function (x,y){ return x-y;});
let n = a.length;
let m = b.length;
let j = m - 1;
for (let i = 0; i < n; i++) {
if (j < 0)
break ;
if (b[j] > a[i]) {
a[i] = b[j];
j--;
}
}
return (a).join( "" );
}
let a = "1234" ;
let b = "4321" ;
document.write(maxValue(a.split( "" ), b.split( "" )));
</script>
|
Complexity Analysis:
- Time Complexity: O(n+m*log(m)) where n is the size of string a and m is the size of string b.
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...