# Sum and product of k smallest and k largest prime numbers in the array

Given an integer *k* and an array of integers *arr*, the task is to find the sum and product of *k* smallest and *k* largest prime numbers in the array.

Assume that there are at least *k* prime numbers in the array.

**Examples:**

Input:arr[] = {2, 5, 6, 8, 10, 11}, k = 2

Output:Sum of k-minimum prime numbers is 7

Sum of k-maximum prime numbers is 16

Product of k-minimum prime numbers is 10

Product of k-maximum prime numbers is 55

{2, 5, 11} are the only prime numbers from the array. {2, 5} are the 2 smallest and {5, 11} are the 2 largest among them.

Input:arr[] = {4, 2, 12, 13, 5, 19}, k = 3

Output:Sum of k-minimum prime numbers is 20

Sum of k-maximum prime numbers is 37

Product of k-minimum prime numbers is 130

Product of k-maximum prime numbers is 1235

**Approach:**

- Using Sieve of Eratosthenes generate a boolean vector upto the size of the maximum element from the array which can be used to check whether a number is prime or not.
- Also set
*0*and*1*as non-prime so that they don’t get counted as prime numbers. - Now traverse the array and insert all the numbers which are prime in two heaps, a min heap and a max heap.
- Now, pop out top
*k*elements from the*min heap*and take the*sum*and*product*of the minimum*k*prime numbers. - Do the same with the
*max heap*to get the*sum*and*product*of the max*k*prime numbers. - Finally, print the results.

Below is the implementation of the above approach:

`// C++ program to find the sum ` `// and product of k smallest and ` `// k largest prime numbers in an array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `vector<` `bool` `> SieveOfEratosthenes(` `int` `max_val) ` `{ ` ` ` `// Create a boolean vector "prime[0..n]". A ` ` ` `// value in prime[i] will finally be false ` ` ` `// if i is Not a prime, else true. ` ` ` `vector<` `bool` `> prime(max_val + 1, ` `true` `); ` ` ` `for` `(` `int` `p = 2; p * p <= max_val; p++) { ` ` ` ` ` `// If prime[p] is not changed, then ` ` ` `// it is a prime ` ` ` `if` `(prime[p] == ` `true` `) { ` ` ` ` ` `// Update all multiples of p ` ` ` `for` `(` `int` `i = p * 2; i <= max_val; i += p) ` ` ` `prime[i] = ` `false` `; ` ` ` `} ` ` ` `} ` ` ` `return` `prime; ` `} ` ` ` `// Function that calculates the sum ` `// and product of k smallest and k ` `// largest prime numbers in an array ` `void` `primeSumAndProduct(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Find maximum value in the array ` ` ` `int` `max_val = *max_element(arr, arr + n); ` ` ` ` ` `// Use sieve to find all prime numbers ` ` ` `// less than or equal to max_val ` ` ` `vector<` `bool` `> prime = SieveOfEratosthenes(max_val); ` ` ` ` ` `// Set 0 and 1 as non-primes as ` ` ` `// they don't need to be ` ` ` `// counted as prime numbers ` ` ` `prime[0] = ` `false` `; ` ` ` `prime[1] = ` `false` `; ` ` ` ` ` `// Max Heap to store all the prime numbers ` ` ` `priority_queue<` `int` `> maxHeap; ` ` ` ` ` `// Min Heap to store all the prime numbers ` ` ` `priority_queue<` `int` `, vector<` `int` `>, greater<` `int` `>> ` ` ` `minHeap; ` ` ` ` ` `// Push all the prime numbers ` ` ` `// from the array to the heaps ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(prime[arr[i]]) { ` ` ` `minHeap.push(arr[i]); ` ` ` `maxHeap.push(arr[i]); ` ` ` `} ` ` ` `long` `long` `int` `minProduct = 1 ` ` ` `, maxProduct = 1 ` ` ` `, minSum = 0 ` ` ` `, maxSum = 0; ` ` ` `while` `(k--) { ` ` ` ` ` `// Calculate the products ` ` ` `minProduct *= minHeap.top(); ` ` ` `maxProduct *= maxHeap.top(); ` ` ` ` ` `// Calculate the sum ` ` ` `minSum += minHeap.top(); ` ` ` `maxSum += maxHeap.top(); ` ` ` ` ` `// Pop the current minimum element ` ` ` `minHeap.pop(); ` ` ` ` ` `// Pop the current maximum element ` ` ` `maxHeap.pop(); ` ` ` `} ` ` ` ` ` `cout << ` `"Sum of k-minimum prime numbers is "` ` ` `<< minSum << ` `"\n"` `; ` ` ` `cout << ` `"Sum of k-maximum prime numbers is "` ` ` `<< maxSum << ` `"\n"` `; ` ` ` `cout << ` `"Product of k-minimum prime numbers is "` ` ` `<< minProduct << ` `"\n"` `; ` ` ` `cout << ` `"Product of k-maximum prime numbers is "` ` ` `<< maxProduct; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 4, 2, 12, 13, 5, 19 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `int` `k = 3; ` ` ` ` ` `primeSumAndProduct(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Sum of k-minimum prime numbers is 20 Sum of k-maximum prime numbers is 37 Product of k-minimum prime numbers is 130 Product of k-maximum prime numbers is 1235

## Recommended Posts:

- Sum and product of k smallest and k largest composite numbers in the array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Product of all prime numbers in an Array
- Check if product of array containing prime numbers is a perfect square
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Numbers less than N which are product of exactly two distinct prime numbers
- Find product of prime numbers between 1 to n
- Find the Product of first N Prime Numbers
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Find two distinct prime numbers with given product
- Largest palindrome which is product of two n-digit numbers
- Print prime numbers with prime sum of digits in an array
- Product of every K’th prime number in an array
- Largest palindromic prime in an array

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.