Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3 | Function to calculate sum of digits of an integer ; Function to calculate maximum sum of array after removing pairs whose concatenation is divisible by 3 ; Stores the sum of digits of array element ; Find the sum of digits ; If i is divisible by 3 ; Otherwise , if i modulo 3 is 1 ; Otherwise , if i modulo 3 is 2 ; Return the resultant sum of array elements ; Driver Code
function getSum ( n ) { let ans = 0 ; let arr = n . toString ( ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { ans += arr [ i ] ; } return ans ; } function getMax ( arr , n ) { let maxRem0 = 0 ; let rem1 = 0 ; let rem2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let digitSum = getSum ( arr [ i ] ) ; if ( digitSum % 3 == 0 ) maxRem0 = Math . max ( maxRem0 , arr [ i ] ) ; else if ( digitSum % 3 == 1 ) rem1 += arr [ i ] ; else rem2 += arr [ i ] ; } document . write ( maxRem0 + Math . max ( rem1 , rem2 ) ) } let arr = [ 23 , 12 , 43 , 3 , 56 ] ; let n = arr . length ; getMax ( arr , n ) ;