Count pairs in array whose sum is divisible by 4 | Python3 code to count pairs whose sum is divisible by '4 ; Function to count pairs whose sum is divisible by '4 ; Create a frequency array to count occurrences of all remainders when divided by 4 ; Count occurrences of all remainders ; If both pairs are divisible by '4 ; If both pairs are 2 modulo 4 ; If one of them is equal to 1 modulo 4 and the other is equal to 3 modulo 4 ; Driver code
' NEW_LINE ' NEW_LINE def count4Divisibiles ( arr , n ) : NEW_LINE INDENT freq = [ 0 , 0 , 0 , 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] % 4 ] += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT ans = freq [ 0 ] * ( freq [ 0 ] - 1 ) / 2 NEW_LINE ans += freq [ 2 ] * ( freq [ 2 ] - 1 ) / 2 NEW_LINE ans += freq [ 1 ] * freq [ 3 ] NEW_LINE return int ( ans ) NEW_LINE DEDENT arr = [ 2 , 2 , 1 , 7 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( count4Divisibiles ( arr , n ) ) NEW_LINE