text: Maximize the value of A by replacing some of its digits with digits of B | C ++ implementation of the approach ; Function to return the maximized value of A ; Sort digits in ascending order ; j points to largest digit in B ; If all the digits of b have been used ; Current digit has been used ; Return the maximized value ; Driver code
code: #include <bits/stdc++.h> NEW_LINE 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 ; }