Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3 | Java approach for the above approach ; 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
class GFG { static int getSum ( int n ) { int ans = 0 ; char [ ] arr = ( String . valueOf ( n ) ) . toCharArray ( ) ; for ( char ch : arr ) { ans += Character . getNumericValue ( ch ) ; } return ans ; } static void getMax ( int [ ] arr ) { int maxRem0 = 0 ; int rem1 = 0 ; int rem2 = 0 ; for ( int i : arr ) { int digitSum = getSum ( i ) ; if ( digitSum % 3 == 0 ) maxRem0 = Math . max ( maxRem0 , i ) ; else if ( digitSum % 3 == 1 ) rem1 += i ; else rem2 += i ; } System . out . print ( maxRem0 + Math . max ( rem1 , rem2 ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 23 , 12 , 43 , 3 , 56 } ; getMax ( arr ) ; } }