Given an array of size **N**. The task to find the maximum possible number of elements divisible by 3 that are in the array after performing the operation an arbitrary (possibly, zero) number of times. In each operation, one can add any two elements of the array.

**Examples:**

Input :a[] = {1, 2, 3}

Output :2

After applying the operation once (on elements 1, 2), the array becomes {3, 3}.

It contains 2 numbers which are divisible by 3 which are maximum possible.

Input :a[] = {1, 1, 1, 1, 1, 2, 2}

Output :3

**Approach :**

Let cnt_{i} be the number of elements of a with the remainder i modulo 3. Then the initial answer can be represented as cnt_{0} and we have to compose numbers with remainders 1 and 2 somehow optimally. It can be shown that the best way to do it is the following:

- Firstly, while there is at least one remainder of 1 and at least one remainder of 2 then compose them into one 0. After this, at least one of the numbers cnt
_{1}, cnt_{2}will be zero, then we have to compose remaining numbers into numbers divisible by 3. - If cnt
_{1}=0 then the maximum remaining number of elements we can obtain is [cnt_{2}/3] (because 2+2+2=6), and in the other case (cnt_{2}=0) the maximum number of elements is [cnt_{1}/3] (because 1+1+1=3).

Below is the implementation of the above approach :

## C++

`// C++ program to find the maximum ` `// number of elements divisible by 3 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum ` `// number of elements divisible by 3 ` `int` `MaxNumbers(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// To store frequency of each number ` ` ` `int` `fre[3] = { 0 }; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// Store modulo value ` ` ` `a[i] %= 3; ` ` ` ` ` `// Strore frequency ` ` ` `fre[a[i]]++; ` ` ` `} ` ` ` ` ` `// Add numbers with zero modulo to answer ` ` ` `int` `ans = fre[0]; ` ` ` ` ` `// Find minimum of elements with modulo ` ` ` `// frequency one and zero ` ` ` `int` `k = min(fre[1], fre[2]); ` ` ` ` ` `// Add k to the answer ` ` ` `ans += k; ` ` ` ` ` `// Remove them from frequency ` ` ` `fre[1] -= k; ` ` ` `fre[2] -= k; ` ` ` ` ` `// Add numbers possible with ` ` ` `// remaining frequency ` ` ` `ans += fre[1] / 3 + fre[2] / 3; ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `a[] = { 1, 4, 10, 7, 11, 2, 8, 5, 9 }; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `// Function call ` ` ` `cout << MaxNumbers(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the maximum ` `// number of elements divisible by 3 ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the maximum ` ` ` `// number of elements divisible by 3 ` ` ` `static` `int` `MaxNumbers(` `int` `a[], ` `int` `n) ` ` ` `{ ` ` ` `// To store frequency of each number ` ` ` `int` `[]fre = { ` `0` `,` `0` `,` `0` `}; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `// Store modulo value ` ` ` `a[i] %= ` `3` `; ` ` ` ` ` `// Strore frequency ` ` ` `fre[a[i]]++; ` ` ` `} ` ` ` ` ` `// Add numbers with zero modulo to answer ` ` ` `int` `ans = fre[` `0` `]; ` ` ` ` ` `// Find minimum of elements with modulo ` ` ` `// frequency one and zero ` ` ` `int` `k = Math.min(fre[` `1` `], fre[` `2` `]); ` ` ` ` ` `// Add k to the answer ` ` ` `ans += k; ` ` ` ` ` `// Remove them from frequency ` ` ` `fre[` `1` `] -= k; ` ` ` `fre[` `2` `] -= k; ` ` ` ` ` `// Add numbers possible with ` ` ` `// remaining frequency ` ` ` `ans += fre[` `1` `] / ` `3` `+ fre[` `2` `] / ` `3` `; ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `4` `, ` `10` `, ` `7` `, ` `11` `, ` `2` `, ` `8` `, ` `5` `, ` `9` `}; ` ` ` ` ` `int` `n = a.length; ` ` ` ` ` `// Function call ` ` ` `System.out.println(MaxNumbers(a, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by @@ajit.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the maximum ` `# number of elements divisible by 3 ` ` ` `# Function to find the maximum ` `# number of elements divisible by 3 ` `def` `MaxNumbers(a, n): ` ` ` ` ` `# To store frequency of each number ` ` ` `fre ` `=` `[` `0` `for` `i ` `in` `range` `(` `3` `)] ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Store modulo value ` ` ` `a[i] ` `%` `=` `3` ` ` ` ` `# Strore frequency ` ` ` `fre[a[i]] ` `+` `=` `1` ` ` ` ` `# Add numbers with zero modulo to answer ` ` ` `ans ` `=` `fre[` `0` `] ` ` ` ` ` `# Find minimum of elements with modulo ` ` ` `# frequency one and zero ` ` ` `k ` `=` `min` `(fre[` `1` `], fre[` `2` `]) ` ` ` ` ` `# Add k to the answer ` ` ` `ans ` `+` `=` `k ` ` ` ` ` `# Remove them from frequency ` ` ` `fre[` `1` `] ` `-` `=` `k ` ` ` `fre[` `2` `] ` `-` `=` `k ` ` ` ` ` `# Add numbers possible with ` ` ` `# remaining frequency ` ` ` `ans ` `+` `=` `fre[` `1` `] ` `/` `/` `3` `+` `fre[` `2` `] ` `/` `/` `3` ` ` ` ` `# Return the required answer ` ` ` `return` `ans ` ` ` `# Driver code ` `a ` `=` `[` `1` `, ` `4` `, ` `10` `, ` `7` `, ` `11` `, ` `2` `, ` `8` `, ` `5` `, ` `9` `] ` ` ` `n ` `=` `len` `(a) ` ` ` `# Function call ` `print` `(MaxNumbers(a, n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the maximum ` `// number of elements divisible by 3 ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the maximum ` ` ` `// number of elements divisible by 3 ` ` ` `static` `int` `MaxNumbers(` `int` `[]a, ` `int` `n) ` ` ` `{ ` ` ` `// To store frequency of each number ` ` ` `int` `[]fre = { 0,0,0 }; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// Store modulo value ` ` ` `a[i] %= 3; ` ` ` ` ` `// Strore frequency ` ` ` `fre[a[i]]++; ` ` ` `} ` ` ` ` ` `// Add numbers with zero modulo to answer ` ` ` `int` `ans = fre[0]; ` ` ` ` ` `// Find minimum of elements with modulo ` ` ` `// frequency one and zero ` ` ` `int` `k = Math.Min(fre[1], fre[2]); ` ` ` ` ` `// Add k to the answer ` ` ` `ans += k; ` ` ` ` ` `// Remove them from frequency ` ` ` `fre[1] -= k; ` ` ` `fre[2] -= k; ` ` ` ` ` `// Add numbers possible with ` ` ` `// remaining frequency ` ` ` `ans += fre[1] / 3 + fre[2] / 3; ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` ` ` `int` `[]a = { 1, 4, 10, 7, 11, 2, 8, 5, 9 }; ` ` ` ` ` `int` `n = a.Length; ` ` ` ` ` `// Function call ` ` ` `Console.WriteLine(MaxNumbers(a, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

5

** Time Complexity : ** O(N)

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:

- Find permutation of n which is divisible by 3 but not divisible by 6
- 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]
- Find set of m-elements with difference of any two elements is divisible by k
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Maximum possible elements which are divisible by 2
- Maximum count of elements divisible on the left for any element
- Maximum sum of elements divisible by K from the given array
- Minimum elements to be added in a range so that count of elements is divisible by K
- Count of elements not divisible by any other elements of Array
- Find the maximum range [L,R] whose sum is divisible by M
- Find if a number is divisible by every number in a list
- Minimum and Maximum element of an array which is divisible by a given number k
- Possible cuts of a number such that maximum parts are divisible by 3
- Maximum splits in binary string such that each substring is divisible by given odd number
- Find an array element such that all elements are divisible by it
- Find prime factors of Array elements whose sum of exponents is divisible by K
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Check if LCM of array elements is divisible by a prime number or not
- Product of all the elements in an array divisible by a given number K
- Sum of all the elements in an array divisible by a given number K

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.