- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Number of Groups of Sizes Two Or Three Divisible By 3 in C++

Given an array of numbers, we need to find the number of groups of size 2 and 3 that are divisible by 3. We can get the sums of two and three combination numbers and check whether they are divisible by 3 or not.

Let's see an example.

**Input**

arr = [1, 2, 3, 4]

**Output**

4

There are 4 combinations that are divisible by 3. The combinations are...

[1, 2] [2, 4] [1, 2, 3] [2, 3, 4]

## Algorithm

Initialise the array.

Write two loops to get all combinations of size two.

Compute the sum of each group.

If the sum is divisible by 3, then increment the count.

Write three loops to get all combinations of size three.

Compute the sum of each group.

If the sum is divisible by 3, then increment the count.

Return the count.

## Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h> using namespace std; int getNumberOfGroupsDivisibleBy3(int arr[], int n) { int count = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int sum = arr[i] + arr[j]; if (sum % 3 == 0) { count += 1; } } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { int sum = arr[i] + arr[j] + arr[k]; if (sum % 3 == 0) { count += 1; } } } } return count; } int main() { int arr[] = { 2, 3, 4, 5, 6, 1, 2, 4, 7, 8 }; int n = 10; cout << getNumberOfGroupsDivisibleBy3(arr, n) << endl; return 0; }

## Output

If you run the above code, then you will get the following result.

57

- Related Articles
- Number of substrings divisible by 8 and not by 3 in C++
- Maximum number of 3-person teams formed from two groups in C++
- Maximum number of groups of size 3 containing two type of items in C++
- Check if a large number is divisible by 3 or not in C++
- Number is divisible by 29 or not in C++
- Largest N digit number divisible by given three numbers in C++
- Number of digits to be removed to make a number divisible by 3 in C++
- Greatest Sum Divisible by Three in C++
- Check if a large number is divisible by 2, 3 and 5 or not in C++
- Check if a large number is divisible by 3 or not in java
- How many numbers of two digit are divisible by 3?
- Possible cuts of a number such that maximum parts are divisible by 3 in C++
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Which of the following statements are true?(i) If a number is divisible by 3, it must be divisible by 9.(ii) If a number is divisible by 9, it must be divisible by 3.(iii) If a number is divisible by 4, it must be divisible by 8.(iv) If a number is divisible by 8, it must be divisible by 4.(v) A number is divisible by 18, if it is divisible by both 3 and 6.(vi) If a number is divisible by both 9 and 10, it must be divisible by 90.(vii) If a number exactly divides the sum of two numbers, it must exactly divide the numbers separately.(viii) If a number divides three numbers exactly, it must divide their sum exactly.(ix) If two numbers are co-prime, at least one of them must be a prime number.(x) The sum of two consecutive odd numbers is always divisible by 4.
- Check if a number is divisible by 23 or not in C++