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
using System ; class GFG { static 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 ; } static int findMaxSum ( int [ ] A , int [ ] B , int n ) { int [ ] maxArr = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { int X = joinNumbers ( A [ i ] , B [ i ] ) ; int Y = joinNumbers ( B [ i ] , A [ i ] ) ; int mx = Math . Max ( X , Y ) ; maxArr [ i ] = mx ; } int maxAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] A = { 11 , 23 , 38 , 43 , 59 } ; int [ ] B = { 36 , 24 , 17 , 40 , 56 } ; Console . WriteLine ( findMaxSum ( A , B , N ) ) ; } }