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++
#include <bits/stdc++.h>
using namespace std;
int MaxNumbers( int a[], int n)
{
int fre[3] = { 0 };
for ( int i = 0; i < n; i++) {
a[i] %= 3;
fre[a[i]]++;
}
int ans = fre[0];
int k = min(fre[1], fre[2]);
ans += k;
fre[1] -= k;
fre[2] -= k;
ans += fre[1] / 3 + fre[2] / 3;
return ans;
}
int main()
{
int a[] = { 1, 4, 10, 7, 11, 2, 8, 5, 9 };
int n = sizeof (a) / sizeof (a[0]);
cout << MaxNumbers(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MaxNumbers( int a[], int n)
{
int []fre = { 0 , 0 , 0 };
for ( int i = 0 ; i < n; i++)
{
a[i] %= 3 ;
fre[a[i]]++;
}
int ans = fre[ 0 ];
int k = Math.min(fre[ 1 ], fre[ 2 ]);
ans += k;
fre[ 1 ] -= k;
fre[ 2 ] -= k;
ans += fre[ 1 ] / 3 + fre[ 2 ] / 3 ;
return ans;
}
public static void main (String[] args)
{
int a[] = { 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 };
int n = a.length;
System.out.println(MaxNumbers(a, n));
}
}
|
Python3
def MaxNumbers(a, n):
fre = [ 0 for i in range ( 3 )]
for i in range (n):
a[i] % = 3
fre[a[i]] + = 1
ans = fre[ 0 ]
k = min (fre[ 1 ], fre[ 2 ])
ans + = k
fre[ 1 ] - = k
fre[ 2 ] - = k
ans + = fre[ 1 ] / / 3 + fre[ 2 ] / / 3
return ans
a = [ 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 ]
n = len (a)
print (MaxNumbers(a, n))
|
C#
using System;
class GFG
{
static int MaxNumbers( int []a, int n)
{
int []fre = { 0,0,0 };
for ( int i = 0; i < n; i++)
{
a[i] %= 3;
fre[a[i]]++;
}
int ans = fre[0];
int k = Math.Min(fre[1], fre[2]);
ans += k;
fre[1] -= k;
fre[2] -= k;
ans += fre[1] / 3 + fre[2] / 3;
return ans;
}
static public void Main ()
{
int []a = { 1, 4, 10, 7, 11, 2, 8, 5, 9 };
int n = a.Length;
Console.WriteLine(MaxNumbers(a, n));
}
}
|
Javascript
<script>
function MaxNumbers(a, n)
{
let fre = new Array(3).fill(0);
for (let i = 0; i < n; i++) {
a[i] %= 3;
fre[a[i]]++;
}
let ans = fre[0];
let k = Math.min(fre[1], fre[2]);
ans += k;
fre[1] -= k;
fre[2] -= k;
ans += parseInt(fre[1] / 3) + parseInt(fre[2] / 3);
return ans;
}
let a = [ 1, 4, 10, 7, 11, 2, 8, 5, 9 ];
let n = a.length;
document.write(MaxNumbers(a, n));
</script>
|
Time Complexity : O(N)
Auxiliary Space: O(1)
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...