Number of Groups of Sizes Two Or Three Divisible By 3 | 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 Function
#include <iostream> NEW_LINE using namespace std ; int numOfCombinations ( int arr [ ] , int N ) { int C [ 3 ] = { 0 , 0 , 0 } ; for ( int 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 ] ; } int main ( ) { int arr1 [ 6 ] = { 1 , 5 , 7 , 2 , 9 , 14 } ; cout << numOfCombinations ( arr1 , 6 ) << " STRNEWLINE " ; int arr2 [ 4 ] = { 3 , 6 , 9 , 12 } ; cout << numOfCombinations ( arr2 , 4 ) << " STRNEWLINE " ; return 0 ; }