# Sum of elements in an array having prime frequency

Given an array **arr**, the task is to find the sum of the elements which have prime frequencies in the array.

**Note:** 1 is neither prime nor composite.

**Examples:**

Input:arr[] = {5, 4, 6, 5, 4, 6}

Output:15

All the elements appear 2 times which is a prime

So, 5 + 4 + 6 = 15

Input:arr[] = {1, 2, 3, 3, 2, 3, 2, 3, 3}

Output:5

Only 2 and 3 appears prime number of times i.e. 3 and 5 respectively.

So, 2 + 3 = 5

**Approach:**

- Traverse the array and store the frequencies of all the elements in a map.
- Build Sieve of Eratosthenes which will be used to test the primality of a number in O(1) time.
- Calculate the sum of elements having prime frequency using the Sieve array calculated in the previous step.

Below is the implementation of the above approach:

## C++

`// C++ program to find sum of elements ` `// in an array having prime frequency ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to create Sieve to check primes ` `void` `SieveOfEratosthenes(` `bool` `prime[], ` `int` `p_size) ` `{ ` ` ` `// False here indicates ` ` ` `// that it is not prime ` ` ` `prime[0] = ` `false` `; ` ` ` `prime[1] = ` `false` `; ` ` ` ` ` `for` `(` `int` `p = 2; p * p <= p_size; p++) { ` ` ` ` ` `// If prime[p] is not changed, ` ` ` `// then it is a prime ` ` ` `if` `(prime[p]) { ` ` ` ` ` `// Update all multiples of p, ` ` ` `// set them to non-prime ` ` ` `for` `(` `int` `i = p * 2; i <= p_size; i += p) ` ` ` `prime[i] = ` `false` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to return the sum of elements ` `// in an array having prime frequency ` `int` `sumOfElements(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `bool` `prime[n + 1]; ` ` ` `memset` `(prime, ` `true` `, ` `sizeof` `(prime)); ` ` ` ` ` `SieveOfEratosthenes(prime, n + 1); ` ` ` ` ` `int` `i, j; ` ` ` ` ` `// Map is used to store ` ` ` `// element frequencies ` ` ` `unordered_map<` `int` `, ` `int` `> m; ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `m[arr[i]]++; ` ` ` ` ` `int` `sum = 0; ` ` ` ` ` `// Traverse the map using iterators ` ` ` `for` `(` `auto` `it = m.begin(); it != m.end(); it++) { ` ` ` ` ` `// Count the number of elements ` ` ` `// having prime frequencies ` ` ` `if` `(prime[it->second]) { ` ` ` `sum += (it->first); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 5, 4, 6, 5, 4, 6 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << sumOfElements(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to find Sum of elements

# in an array having prime frequency

import math as mt

# Function to create Sieve to

# check primes

def SieveOfEratosthenes(prime, p_size):

# False here indicates

# that it is not prime

prime[0] = False

prime[1] = False

for p in range(2, mt.ceil(mt.sqrt(p_size + 1))):

# If prime[p] is not changed,

# then it is a prime

if (prime[p]):

# Update all multiples of p,

# set them to non-prime

for i in range(p * 2, p_size + 1, p):

prime[i] = False

# Function to return the Sum of elements

# in an array having prime frequency

def SumOfElements(arr, n):

prime = [True for i in range(n + 1)]

SieveOfEratosthenes(prime, n + 1)

i, j = 0, 0

# Map is used to store

# element frequencies

m = dict()

for i in range(n):

if arr[i] in m.keys():

m[arr[i]] += 1

else:

m[arr[i]] = 1

Sum = 0

# Traverse the map using iterators

for i in m:

# Count the number of elements

# having prime frequencies

if (prime[m[i]]):

Sum += (i)

return Sum

# Driver code

arr = [5, 4, 6, 5, 4, 6 ]

n = len(arr)

print(SumOfElements(arr, n))

# This code is contributed

# by Mohit kumar 29

**Output:**

15

## Recommended Posts:

- Sum of all odd frequency elements in an array
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Find the element having different frequency than other array elements
- Array range queries for elements with frequency same as value
- Replace every elements in the array by its frequency in the array
- Fill an array based on frequency where elements are in range from 0 to n-1
- Remove elements from the array whose frequency lies in the range [l, r]
- Check whether the sum of prime elements of the array is prime or not
- Array elements with prime frequencies
- Find the sum of non-prime elements in the given array
- Prime factors of LCM of array elements
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Check if LCM of array elements is divisible by a prime number or not
- Check if elements of array can be made equal by multiplying given prime numbers

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.