Count maximum concatenation of pairs from given array that are divisible by 3 | Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count of array elements whose remainder is 0 by taking modulo by 3 ; Stores count of array elements whose remainder is 1 by taking modulo by 3 ; Stores count of array elements whose remainder is 2 by taking modulo by 3 ; Traverse the array ; Stores sum of digits of arr [ i ] ; Update digitSum ; If remainder of digitSum by by taking modulo 3 is 0 ; Update rem0 ; If remainder of digitSum by by taking modulo 3 is 1 ; Update rem1 ; Update rem2 ; Driver Code ; To display the result
def countDiv ( arr ) : NEW_LINE INDENT rem0 = 0 NEW_LINE rem1 = 0 NEW_LINE rem2 = 0 NEW_LINE for i in arr : NEW_LINE INDENT digitSum = 0 NEW_LINE for digit in str ( i ) : NEW_LINE INDENT digitSum += int ( digit ) NEW_LINE DEDENT if digitSum % 3 == 0 : NEW_LINE INDENT rem0 += 1 NEW_LINE DEDENT elif digitSum % 3 == 1 : NEW_LINE INDENT rem1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT rem2 += 1 NEW_LINE DEDENT DEDENT return ( rem0 // 2 + min ( rem1 , rem2 ) ) NEW_LINE DEDENT arr = [ 5 , 3 , 2 , 8 , 7 ] NEW_LINE print ( countDiv ( arr ) ) NEW_LINE