Maximize array sum by concatenating corresponding elements of given two arrays | Function to join the two numbers ; Loop to reverse the digits of the one number ; Loop to join two numbers ; Function to find the maximum array sum ; Loop to iterate over the two elements of the array ; Find the array sum ; Return the array sum ; Driver Code
def joinNumbers ( numA , numB ) : NEW_LINE INDENT revB = 0 NEW_LINE while ( numB > 0 ) : NEW_LINE INDENT revB = revB * 10 + ( numB % 10 ) NEW_LINE numB = numB // 10 NEW_LINE DEDENT while ( revB > 0 ) : NEW_LINE INDENT numA = numA * 10 + ( revB % 10 ) NEW_LINE revB = revB // 10 NEW_LINE DEDENT return numA NEW_LINE DEDENT def findMaxSum ( A , B , n ) : NEW_LINE INDENT maxArr = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT X = joinNumbers ( A [ i ] , B [ i ] ) NEW_LINE Y = joinNumbers ( B [ i ] , A [ i ] ) NEW_LINE mx = max ( X , Y ) NEW_LINE maxArr [ i ] = mx NEW_LINE DEDENT maxAns = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxAns += maxArr [ i ] NEW_LINE DEDENT return maxAns NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE A = [ 11 , 23 , 38 , 43 , 59 ] NEW_LINE B = [ 36 , 24 , 17 , 40 , 56 ] NEW_LINE print ( findMaxSum ( A , B , N ) ) NEW_LINE DEDENT