Find the maximum number of elements divisible by 3 | Function to find the maximum number of elements divisible by 3 ; To store frequency of each number ; Store modulo value ; Store frequency ; Add numbers with zero modulo to answer ; Find minimum of elements with modulo frequency one and zero ; Add k to the answer ; Remove them from frequency ; Add numbers possible with remaining frequency ; Return the required answer ; Driver code ; Function call
def MaxNumbers ( a , n ) : NEW_LINE INDENT fre = [ 0 for i in range ( 3 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] %= 3 NEW_LINE fre [ a [ i ] ] += 1 NEW_LINE DEDENT ans = fre [ 0 ] NEW_LINE k = min ( fre [ 1 ] , fre [ 2 ] ) NEW_LINE ans += k NEW_LINE fre [ 1 ] -= k NEW_LINE fre [ 2 ] -= k NEW_LINE ans += fre [ 1 ] // 3 + fre [ 2 ] // 3 NEW_LINE return ans NEW_LINE DEDENT a = [ 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 ] NEW_LINE n = len ( a ) NEW_LINE print ( MaxNumbers ( a , n ) ) NEW_LINE