Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3 | C # program 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
using System ; class GFG { static int getSum ( int n ) { int ans = 0 ; string str = n . ToString ( ) ; Char [ ] arr = str . ToCharArray ( ) ; foreach ( Char ch in arr ) { ans += ( int ) Char . GetNumericValue ( ch ) ; } return ans ; } static void getMax ( int [ ] arr ) { int maxRem0 = 0 ; int rem1 = 0 ; int rem2 = 0 ; foreach ( int i in arr ) { int digitSum = getSum ( i ) ; if ( digitSum % 3 == 0 ) maxRem0 = Math . Max ( maxRem0 , i ) ; else if ( digitSum % 3 == 1 ) rem1 += i ; else rem2 += i ; } Console . WriteLine ( maxRem0 + Math . Max ( rem1 , rem2 ) ) ; } static void Main ( ) { int [ ] arr = { 23 , 12 , 43 , 3 , 56 } ; getMax ( arr ) ; } }