Number of Groups of Sizes Two Or Three Divisible By 3 | PHP Program to find groups of 2 or 3 whose sum is divisible by 3 ; Initialize groups to 0 ; Increment group with specified remainder ; Return groups using the formula ; Driver Code
< ? php function numOfCombinations ( $ arr , $ N ) { $ C = array ( 0 , 0 , 0 ) ; for ( $ i = 0 ; $ i < $ N ; ++ $ i ) ++ $ C [ $ arr [ $ i ] % 3 ] ; return $ C [ 1 ] * $ C [ 2 ] + $ C [ 0 ] * ( $ C [ 0 ] - 1 ) / 2 + $ C [ 0 ] * ( $ C [ 0 ] - 1 ) * ( $ C [ 0 ] - 2 ) / 6 + $ C [ 1 ] * ( $ C [ 1 ] - 1 ) * ( $ C [ 1 ] - 2 ) / 6 + $ C [ 2 ] * ( $ C [ 2 ] - 1 ) * ( $ C [ 2 ] - 2 ) / 6 + $ C [ 0 ] * $ C [ 1 ] * $ C [ 2 ] ; } $ arr1 = array ( 1 , 5 , 7 , 2 , 9 , 14 ) ; echo numOfCombinations ( $ arr1 , 6 ) , " STRNEWLINE " ; $ arr2 = array ( 3 , 6 , 9 , 12 ) ; echo numOfCombinations ( $ arr2 , 4 ) , " STRNEWLINE " ; ? >