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
function joinNumbers ( numA , numB ) { var revB = 0 ; while ( numB > 0 ) { revB = revB * 10 + ( numB % 10 ) ; numB = parseInt ( numB / 10 ) ; } while ( revB > 0 ) { numA = numA * 10 + ( revB % 10 ) ; revB = parseInt ( revB / 10 ) ; } return numA ; } function findMaxSum ( A , B , n ) { var maxArr = Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; ++ i ) { var X = joinNumbers ( A [ i ] , B [ i ] ) ; var Y = joinNumbers ( B [ i ] , A [ i ] ) ; var mx = Math . max ( X , Y ) ; maxArr [ i ] = mx ; } var maxAns = 0 ; for ( var i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } var N = 5 ; var A = [ 11 , 23 , 38 , 43 , 59 ] ; var B = [ 36 , 24 , 17 , 40 , 56 ] ; document . write ( findMaxSum ( A , B , N ) ) ;