Find the maximum number of elements divisible by 3 - GeeksforGeeks

14 May, 2021
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 cnti be the number of elements of a with the remainder i modulo 3. Then the initial answer can be represented as cnt0 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 cnt1, cnt2 will be zero, then we have to compose remaining numbers into numbers divisible by 3.
If cnt1=0 then the maximum remaining number of elements we can obtain is [cnt2/3] (because 2+2+2=6), and in the other case (cnt2=0) the maximum number of elements is [cnt1/3] (because 1+1+1=3).
Below is the implementation of the above approach :  
C++
Java
Python3
C#
Javascript
// 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 3int 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;         // Store 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 codeint 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;}
// Java program to find the maximum// number of elements divisible by 3import 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;                 // Store 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..
# Python3 program to find the maximum# number of elements divisible by 3 # Function to find the maximum# number of elements divisible by 3def 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         # Store 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 codea = [1, 4, 10, 7, 11, 2, 8, 5, 9] n = len(a) # Function callprint(MaxNumbers(a, n)) # This code is contributed by Mohit Kumar
// C# program to find the maximum// number of elements divisible by 3using 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;                 // Store 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
<script> // Javascript program to find the maximum// number of elements divisible by 3 // Function to find the maximum// number of elements divisible by 3function MaxNumbers(a, n){    // To store frequency of each number    let fre = new Array(3).fill(0);     for (let i = 0; i < n; i++) {        // Store modulo value        a[i] %= 3;         // Store frequency        fre[a[i]]++;    }     // Add numbers with zero modulo to answer    let ans = fre[0];     // Find minimum of elements with modulo    // frequency one and zero    let 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 += parseInt(fre[1] / 3) + parseInt(fre[2] / 3);     // Return the required answer    return ans;} // Driver code     let a = [ 1, 4, 10, 7, 11, 2, 8, 5, 9 ];     let n = a.length;     // Function call    document.write(MaxNumbers(a, n)); </script>
5
 
Time Complexity : O(N) 
mohit kumar 29
ankthon
jit_t
rishavmahato348
surinderdawra388
frequency-counting
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7