# Count all possible groups of size 2 or 3 that have sum as multiple of 3 in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this tutorial, we will be discussing a program to find the number of possible groups of size 2 or 3 that have sum as multiple of 3.

In this tutorial, we will be discussing a program to find the number of possible groups of size 2 or 3 that have sum as multiple of 3.

## Example

#include<bits/stdc++.h>
using namespace std;
//returning count of pairs of
//2 or 3
int count_groups(int arr[], int n){
int c = {0}, i;
int res = 0;
for (i=0; i<n; i++)
c[arr[i]%3]++;
res += ((c*(c-1))>>1);
res += c * c;
res += (c * (c-1) * (c-2))/6;
res += (c * (c-1) * (c-2))/6;
res += ((c*(c-1)*(c-2))/6);
res += c*c*c;
return res;
}
int main(){
int arr[] = {3, 6, 7, 2, 9};
int n = sizeof(arr)/sizeof(arr);
cout << "Required number of groups are " << count_groups(arr,n) << endl;
return 0;
}

## Output

Required number of groups are 8