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 pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Check if an element present in any pair or not ; Generate all possible pairs ; If the element already present in a pair ; If the element already present in a pair ; If concatenation of elements is divisible by 3 ; Update ans ; Mark i is True ; Mark j is True ; Driver Code ; To display the result
def countDivBy3InArray ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE taken = [ False ] * len ( arr ) NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if taken [ i ] : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( i + 1 , len ( arr ) ) : NEW_LINE INDENT if taken [ j ] : NEW_LINE INDENT continue NEW_LINE DEDENT if ( not int ( str ( arr [ i ] ) + str ( arr [ j ] ) ) % 3 or not int ( str ( arr [ j ] ) + str ( arr [ i ] ) ) % 3 ) : NEW_LINE INDENT ans += 1 NEW_LINE taken [ i ] = True NEW_LINE taken [ j ] = True NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 3 , 2 , 8 , 7 ] NEW_LINE print ( countDivBy3InArray ( arr ) ) NEW_LINE