Open In App

Find the maximum number of elements divisible by 3

Improve
Improve
Like Article
Like
Save
Share
Report

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 :
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 :
 

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++




// 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;
 
        // 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 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;
}


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;
     
            // 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




# 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
 
        # 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 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


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;
     
            // 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


Javascript




<script>
 
// Javascript program to find the maximum
// number of elements divisible by 3
 
// Function to find the maximum
// number of elements divisible by 3
function 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>


Output: 

5

 

Time Complexity : O(N)

Auxiliary Space: O(1)
 



Last Updated : 31 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads