You are given N distinct numbers. You are tasked with finding the number of groups of 2 or 3 that can be formed whose sum is divisible by three.

**Examples :**

Input : 1 5 7 2 9 14 Output : 13 The groups of two that can be formed are: (1, 5) (5, 7) (1, 2) (2, 7) (1, 14) (7, 14) The groups of three are: (1, 5, 9) (5, 7, 9) (1, 2, 9) (2, 7, 9) (2, 5, 14) (1, 9, 14) (7, 9, 14) Input : 3 6 9 12 Output : 10 All groups of 2 and 3 are valid.

**Naive Approach : ** For each number, we can add it up with every other number and see if the sum is divisible by 3. We then store these sums, so that we can add each number again to check for groups of three.

Time Complexity: O(N^2) for groups of 2, O(N^3) for groups of 3

Auxiliary Space: O(N^2)

**Optimum Approach**

If we carefully look at every number, we realize that 3 options exist:

- The number is divisible by 3
- The number leaves a remainder of 1, when divided by 3
- The number leaves a remainder of 2, when divided by 3

Now, for groups of two being divisible by 3, either both number have to belong to category 1 (both are divisible by 3), or one number should leave a remainder 1, and the other a remainder 2. This way the remainders add up to 3, making the sum divisible by 3.

To form a group of three, either all three numbers should give the same remainder, or one should give remainder 0, another should give 1, and the last should give 2.

In this way, we do not care about the numbers themselves, but their respective remainders. Thus by grouping them into three categories, we can find the total possible groups using a simple formula.

Let C1 be number of elements divisible by 3.

Let C2 be number of elements leaving remainder 1.

Let C3 be number of elements leaving remainder 2.

Answer = C2 * C3 + C1 * (C1 - 1) / 2 --> Groups of 2 + C1 * (C1 - 1) * (C1 - 2) / 6 + C2 * (C2 - 1) * (C2 - 2) / 6 + C3 * (C3 - 1) * (C3 - 2) / 6 --> Groups of 3 with elements of same remainder + C1 * C2 * C3 --> Groups of three with all distinct remainders

## CPP

`// Program to find groups of 2 or 3 ` `// whose sum is divisible by 3 ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `numOfCombinations(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `// Initialize groups to 0 ` ` ` `int` `C[3] = { 0, 0, 0 }; ` ` ` ` ` `// Increment group with specified remainder ` ` ` `for` `(` `int` `i = 0; i < N; ++i) ` ` ` `++C[arr[i] % 3]; ` ` ` ` ` `// Return groups using the formula ` ` ` `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]; ` `} ` ` ` `// Driver Function ` `int` `main() ` `{ ` ` ` `int` `arr1[6] = { 1, 5, 7, 2, 9, 14 }; ` ` ` `cout << numOfCombinations(arr1, 6) << ` `"\n"` `; ` ` ` `int` `arr2[4] = { 3, 6, 9, 12 }; ` ` ` `cout << numOfCombinations(arr2, 4) << ` `"\n"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Program to find groups of 2 or 3 ` `// whose sum is divisible by 3 ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `numOfCombinations(` `int` `arr[], ` `int` `N) ` ` ` `{ ` ` ` `// Initialize groups to 0 ` ` ` `int` `C[] = { ` `0` `, ` `0` `, ` `0` `}; ` ` ` ` ` `// Increment group with specified remainder ` ` ` `for` `(` `int` `i = ` `0` `; i < N; ++i) ` ` ` `++C[arr[i] % ` `3` `]; ` ` ` ` ` `// Return groups using the formula ` ` ` `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` `]; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr1[] = { ` `1` `, ` `5` `, ` `7` `, ` `2` `, ` `9` `, ` `14` `}; ` ` ` `System.out.print(numOfCombinations(arr1, ` `6` `) + ` `"\n"` `); ` ` ` `int` `arr2[] = { ` `3` `, ` `6` `, ` `9` `, ` `12` `}; ` ` ` `System.out.print(numOfCombinations(arr2, ` `4` `) + ` `"\n"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Program to find groups of 2 or 3 ` `# whose sum is divisible by 3 ` ` ` `def` `numOfCombinations(arr, N): ` ` ` `# Initialize groups to 0 ` ` ` `C ` `=` `[` `0` `, ` `0` `, ` `0` `] ` ` ` ` ` `# Increment group with ` ` ` `# specified remainder ` ` ` `for` `i ` `in` `range` `(N): ` ` ` ` ` `C[arr[i] ` `%` `3` `]` `=` `C[arr[i] ` `%` `3` `]` `+` `1` ` ` ` ` `# Return groups using the formula ` ` ` `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` `]) ` ` ` `# Driver code ` ` ` `arr1 ` `=` `[` `1` `, ` `5` `, ` `7` `, ` `2` `, ` `9` `, ` `14` `] ` `print` `(` `int` `(numOfCombinations(arr1, ` `6` `))) ` `arr2 ` `=` `[` `3` `, ` `6` `, ` `9` `, ` `12` `] ` `print` `(` `int` `(numOfCombinations(arr2, ` `4` `))) ` ` ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find groups of 2 or ` `// 3 whose sum is divisible by 3 ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find number of combinations ` ` ` `static` `int` `numOfCombinations(` `int` `[] arr, ` ` ` `int` `N) ` ` ` `{ ` ` ` ` ` `// Initialize groups to 0 ` ` ` `int` `[] C = { 0, 0, 0 }; ` ` ` ` ` `// Increment group with specified remainder ` ` ` `for` `(` `int` `i = 0; i < N; ++i) ` ` ` `++C[arr[i] % 3]; ` ` ` ` ` `// Return groups using the formula ` ` ` `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]; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr1 = { 1, 5, 7, 2, 9, 14 }; ` ` ` `Console.WriteLine(numOfCombinations(arr1, 6)); ` ` ` `int` `[] arr2 = { 3, 6, 9, 12 }; ` ` ` `Console.WriteLine(numOfCombinations(arr2, 4)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` ` ` `// PHP Program to find ` `// groups of 2 or 3 ` `// whose sum is divisible by 3 ` ` ` ` ` `function` `numOfCombinations(` `$arr` `, ` `$N` `) ` `{ ` ` ` `// Initialize groups to 0 ` ` ` `$C` `= ` `array` `(0, 0, 0); ` ` ` ` ` `// Increment group with ` ` ` `// specified remainder ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$N` `; ++` `$i` `) ` ` ` `++` `$C` `[` `$arr` `[` `$i` `] % 3]; ` ` ` ` ` `// Return groups using the formula ` ` ` `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]; ` `} ` ` ` `// Driver Code ` `$arr1` `= ` `array` `(1, 5, 7, 2, 9, 14); ` `echo` `numOfCombinations(` `$arr1` `, 6), ` `"\n"` `; ` ` ` `$arr2` `= ` `array` `(3, 6, 9, 12); ` ` ` `echo` `numOfCombinations(` `$arr2` `, 4), ` `"\n"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

13 10

Asked in Amazon

This article is contributed by **Aditya Kamath**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Ways of dividing a group into two halves such that two elements are in different groups
- Smallest n digit number divisible by given three numbers
- Largest N digit number divisible by given three numbers
- Prove that atleast one of three consecutive even numbers is divisible by 6
- Subsequences of size three in an array whose sum is divisible by m
- Divide 1 to n into two groups with minimum sum difference
- Partition the array into two odd length groups with minimized absolute difference between their median
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Count the number of ways to divide N in k groups incrementally
- Maximize number of groups formed with size not smaller than its largest element
- Number of ways of distributing N identical objects in R distinct groups
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find third number such that sum of all three number becomes prime
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count all possible groups of size 2 or 3 that have sum as multiple of 3
- Reverse an array in groups of given size
- Split N^2 numbers into N groups of equal sum