给定大小为N的数组。执行任意次数(可能为零)的操作后,查找数组中3的最大可除数元素的任务。在每个操作中,可以添加数组的任何两个元素。
例子:
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
方法 :
令cnt i为a的元素数,其余数为i模3。然后初始答案可以表示为cnt 0 ,我们必须以最优的方式将余数1和2组成数。可以证明,做到这一点的最佳方法如下:
- 首先,当至少有一个1的余数和至少一个2的余数,然后将它们组合为一个0。此后,数字cnt 1 ,cnt 2中的至少一个将为零,然后我们必须组合余数变成可被3整除的数字
- 如果cnt 1 = 0,那么我们可以获得的最大元素剩余数为[cnt 2/3 ](因为2 + 2 + 2 = 6),而在另一种情况下(cnt 2 = 0),最大元素数为[cnt 2/3 ]。 [CNT 1/3](因为1 + 1 + 1 = 3)。
下面是上述方法的实现:
C++
// C++ program to find the maximum
// number of elements divisible by 3
#include
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;
}
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..
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
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
输出:
5
时间复杂度: O(N)