给定一个大小为N的整数arr数组,任务是找到数组中具有复合频率的元素的总和。
例子:
Input: arr[] = {1, 2, 1, 1, 1, 3, 3, 2}
Output: 1
1 appears 4 times which is a composite. All other elements 2 and 3 appears 2 times which is prime. So, the answer is just 1.
Input: arr[] = {4, 6, 7}
Output: 0
All elements 4, 6 and 7 appears 1 times which is neither prime nor compsoite. So, the answer is 0.
方法:
- 遍历数组并将所有元素的频率存储在地图中。
- 构建 Eratosthenes 筛,用于在 O(1) 时间内测试数字的素性。
- 使用上一步中计算的 Sieve 阵列计算具有复合频率的元素的总和。
下面是上述方法的实现:
C++
// C++ program to find sum of elements
// in an array having composite frequency
#include
using namespace std;
#define N 100005
// Function to create
// Sieve to check primes
void SieveOfEratosthenes(
vector& composite)
{
for (int i = 0; i < N; i++)
composite[i] = false;
for (int p = 2; p * p < N; p++) {
// If composite[p] is not changed,
// then it is a prime
if (!composite[p]) {
// Update all multiples of p,
// set them to composite
for (int i = p * 2; i < N; i += p)
composite[i] = true;
}
}
}
// Function to return the sum of elements
// in an array having composite frequency
int sumOfElements(
int arr[], int n)
{
vector composite(N);
SieveOfEratosthenes(composite);
// Map is used to store
// element frequencies
unordered_map m;
for (int i = 0; i < n; i++)
m[arr[i]]++;
// To store sum
int sum = 0;
// Traverse the map using iterators
for (auto it = m.begin();
it != m.end(); it++) {
// Count the number of elements
// having composite frequencies
if (composite[it->second]) {
sum += (it->first);
}
}
return sum;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 1, 1, 1,
3, 3, 2, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << sumOfElements(arr, n);
return 0;
}
Java
// Java program to find sum of elements
// in an array having composite frequency
import java.util.*;
class GFG{
static final int N = 10005;
// Function to create
// Sieve to check primes
static void SieveOfEratosthenes(Vector composite)
{
for (int i = 0; i < N; i++)
{
composite.add(i, false);
}
for (int p = 2; p * p < N; p++) {
// If composite[p] is not changed,
// then it is a prime
if (!composite.get(p)) {
// Update all multiples of p,
// set them to composite
for (int i = p * 2; i < N; i += p) {
composite.add(i, true);
}
}
}
}
// Function to return the sum of elements
// in an array having composite frequency
static int sumOfElements(int arr[], int n)
{
Vector composite = new Vector();
for (int i = 0; i < N; i++)
composite.add(false);
SieveOfEratosthenes(composite);
// Map is used to store
// element frequencies
HashMap mp = new HashMap();
for (int i = 0; i < n; i++)
if(mp.containsKey(arr[i])){
mp.put(arr[i], mp.get(arr[i]) + 1);
}
else{
mp.put(arr[i], 1);
}
// To store sum
int sum = 0;
// Traverse the map using iterators
for (Map.Entry it : mp.entrySet()){
// Count the number of elements
// having composite frequencies
if (composite.get(it.getValue())) {
sum += (it.getKey());
}
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 1, 1, 1,
3, 3, 2, 4 };
int n = arr.length;
// Function call
System.out.print(sumOfElements(arr, n));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program to find sum of elements
# in an array having composite frequency
N = 100005
# Function to create
# Sieve to check primes
def SieveOfEratosthenes(composite):
for p in range(2, N):
if p*p > N:
break
# If composite[p] is not changed,
# then it is a prime
if (composite[p] == False):
# Update all multiples of p,
# set them to composite
for i in range(2*p, N, p):
composite[i] = True
# Function to return the sum of elements
# in an array having composite frequency
def sumOfElements(arr, n):
composite = [False] * N
SieveOfEratosthenes(composite)
# Map is used to store
# element frequencies
m = dict();
for i in range(n):
m[arr[i]] = m.get(arr[i], 0) + 1
# To store sum
sum = 0
# Traverse the map using iterators
for it in m:
# Count the number of elements
# having composite frequencies
if (composite[m[it]]):
sum += (it)
return sum
# Driver code
if __name__ == '__main__':
arr=[1, 2, 1, 1, 1,3, 3, 2, 4]
n = len(arr)
# Function call
print(sumOfElements(arr, n))
# This code is contributed by mohit kumar 29
C#
// C# program to find sum of elements
// in an array having composite frequency
using System;
using System.Collections.Generic;
class GFG{
static readonly int N = 10005;
// Function to create
// Sieve to check primes
static void SieveOfEratosthenes(List composite)
{
for (int i = 0; i < N; i++)
{
composite.Insert(i, false);
}
for (int p = 2; p * p < N; p++) {
// If composite[p] is not changed,
// then it is a prime
if (!composite[p]) {
// Update all multiples of p,
// set them to composite
for (int i = p * 2; i < N; i += p) {
composite.Insert(i, true);
}
}
}
}
// Function to return the sum of elements
// in an array having composite frequency
static int sumOfElements(int []arr, int n)
{
List composite = new List();
for (int i = 0; i < N; i++)
composite.Add(false);
SieveOfEratosthenes(composite);
// Map is used to store
// element frequencies
Dictionary mp = new Dictionary();
for (int i = 0; i < n; i++)
if(mp.ContainsKey(arr[i])){
mp[arr[i]] = mp[arr[i]] + 1;
}
else{
mp.Add(arr[i], 1);
}
// To store sum
int sum = 0;
// Traverse the map using iterators
foreach (KeyValuePair it in mp){
// Count the number of elements
// having composite frequencies
if (composite[it.Value]) {
sum += (it.Key);
}
}
return sum;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 2, 1, 1, 1,
3, 3, 2, 4 };
int n = arr.Length;
// Function call
Console.Write(sumOfElements(arr, n));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
1
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live