Number of Groups of Sizes Two Or Three Divisible By 3 | Javascript 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
function numOfCombinations ( arr , N ) { let C = [ 0 , 0 , 0 ] ; for ( let 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 ] ; } let arr1 = [ 1 , 5 , 7 , 2 , 9 , 14 ] ; document . write ( numOfCombinations ( arr1 , 6 ) + " " ) ; let arr2 = [ 3 , 6 , 9 , 12 ] ; document . write ( numOfCombinations ( arr2 , 4 ) + " " ) ;