Find the maximum number of elements divisible by 3

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Output:

5

Time Complexity : O(N)



My Personal Notes arrow_drop_up

pawanasipugmailcom

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.