Maximize the value of A by replacing some of its digits with digits of B | 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 ; Convert array into string ; Return the maximized value ; Driver code
< ? 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 NEW_LINE $ a = str_split ( "1234" ) ; $ b = str_split ( "4321" ) ; echo maxValue ( $ a , $ b ) ; ? >