Maximize array sum by concatenating corresponding elements of given two arrays | C ++ implementation to find the maximum 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int joinNumbers ( int numA , int numB ) { int revB = 0 ; while ( numB > 0 ) { revB = revB * 10 + ( numB % 10 ) ; numB = numB / 10 ; } while ( revB > 0 ) { numA = numA * 10 + ( revB % 10 ) ; revB = revB / 10 ; } return numA ; } int findMaxSum ( int A [ ] , int B [ ] , int n ) { int maxArr [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { int X = joinNumbers ( A [ i ] , B [ i ] ) ; int Y = joinNumbers ( B [ i ] , A [ i ] ) ; int mx = max ( X , Y ) ; maxArr [ i ] = mx ; } int maxAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } int main ( ) { int N = 5 ; int A [ 5 ] = { 11 , 23 , 38 , 43 , 59 } ; int B [ 5 ] = { 36 , 24 , 17 , 40 , 56 } ; cout << findMaxSum ( A , B , N ) ; }