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
def getSum ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in str ( n ) : NEW_LINE INDENT ans += int ( i ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def getMax ( arr ) : NEW_LINE INDENT maxRem0 = 0 NEW_LINE rem1 = 0 NEW_LINE rem2 = 0 NEW_LINE for i in arr : NEW_LINE INDENT digitSum = getSum ( i ) NEW_LINE if digitSum % 3 == 0 : NEW_LINE INDENT maxRem0 = max ( maxRem0 , i ) NEW_LINE DEDENT elif digitSum % 3 == 1 : NEW_LINE INDENT rem1 += i NEW_LINE DEDENT else : NEW_LINE INDENT rem2 += i NEW_LINE DEDENT DEDENT print ( maxRem0 + max ( rem1 , rem2 ) ) NEW_LINE DEDENT arr = [ 23 , 12 , 43 , 3 , 56 ] NEW_LINE getMax ( arr ) NEW_LINE