Count pairs in array whose sum is divisible by 4 | Program to count pairs whose sum 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
< ? php function count4Divisibiles ( $ arr , $ n ) { $ freq = array ( 0 , 0 , 0 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) ++ $ freq [ $ arr [ $ i ] % 4 ] ; $ ans = $ freq [ 0 ] * ( $ freq [ 0 ] - 1 ) / 2 ; $ ans += $ freq [ 2 ] * ( $ freq [ 2 ] - 1 ) / 2 ; $ ans += $ freq [ 1 ] * $ freq [ 3 ] ; return $ ans ; } $ arr = array ( 2 , 2 , 1 , 7 , 5 ) ; $ n = sizeof ( $ arr ) ; echo count4Divisibiles ( $ arr , $ n ) ; ? >