Product of every K’th prime number in an array
Given an integer ‘k’ and an array of integers ‘arr’ (less than 10^6), the task is to find the product of every K’th prime number in the array.
Examples:
Input: arr = {2, 3, 5, 7, 11}, k = 2
Output: 21
All the elements of the array are prime. So, the prime numbers after every K (i.e. 2) interval are 3, 7 and their product is 21.
Input: arr = {41, 23, 12, 17, 18, 19}, k = 2
Output: 437
A simple approach: Traverse the array and find every K’th prime number in the array and calculate the running product. In this way, we’ll have to check every element of the array whether it is prime or not which will take more time as the size of the array increases.
Efficient approach: Create a sieve which will store whether a number is prime or not. Then, it can be used to check a number against prime in O(1) time. In this way, we only have to keep track of every K’th prime number and maintain the running product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
bool prime[MAX + 1];
void SieveOfEratosthenes()
{
memset (prime, true , sizeof (prime));
prime[1] = false ;
prime[0] = false ;
for ( int p = 2; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
void productOfKthPrimes( int arr[], int n, int k)
{
int c = 0;
long long int product = 1;
for ( int i = 0; i < n; i++) {
if (prime[arr[i]]) {
c++;
if (c % k == 0) {
product *= arr[i];
c = 0;
}
}
}
cout << product << endl;
}
int main()
{
SieveOfEratosthenes();
int n = 5, k = 2;
int arr[n] = { 2, 3, 5, 7, 11 };
productOfKthPrimes(arr, n, k);
return 0;
}
|
Java
class GFG
{
static int MAX= 1000000 ;
static boolean [] prime= new boolean [MAX + 1 ];
static void SieveOfEratosthenes()
{
prime[ 1 ] = true ;
prime[ 0 ] = true ;
for ( int p = 2 ; p * p <= MAX; p++) {
if (prime[p] == false ) {
for ( int i = p * 2 ; i <= MAX; i += p)
prime[i] = true ;
}
}
}
static void productOfKthPrimes( int arr[], int n, int k)
{
int c = 0 ;
int product = 1 ;
for ( int i = 0 ; i < n; i++) {
if (!prime[arr[i]]) {
c++;
if (c % k == 0 ) {
product *= arr[i];
c = 0 ;
}
}
}
System.out.println(product);
}
public static void main(String[] args)
{
SieveOfEratosthenes();
int n = 5 , k = 2 ;
int [] arr= new int []{ 2 , 3 , 5 , 7 , 11 };
productOfKthPrimes(arr, n, k);
}
}
|
Python 3
MAX = 1000000
prime = [ True ] * ( MAX + 1 )
def SieveOfEratosthenes():
prime[ 1 ] = False ;
prime[ 0 ] = False ;
p = 2
while p * p < = MAX :
if (prime[p] = = True ):
for i in range (p * 2 , MAX + 1 , p):
prime[i] = False
p + = 1
def productOfKthPrimes(arr, n, k):
c = 0
product = 1
for i in range ( n):
if (prime[arr[i]]):
c + = 1
if (c % k = = 0 ) :
product * = arr[i]
c = 0
print (product)
if __name__ = = "__main__" :
SieveOfEratosthenes()
n = 5
k = 2
arr = [ 2 , 3 , 5 , 7 , 11 ]
productOfKthPrimes(arr, n, k)
|
C#
class GFG
{
static int MAX = 1000000;
static bool [] prime = new bool [MAX + 1];
static void SieveOfEratosthenes()
{
prime[1] = true ;
prime[0] = true ;
for ( int p = 2; p * p <= MAX; p++)
{
if (prime[p] == false )
{
for ( int i = p * 2;
i <= MAX; i += p)
prime[i] = true ;
}
}
}
static void productOfKthPrimes( int [] arr,
int n, int k)
{
int c = 0;
int product = 1;
for ( int i = 0; i < n; i++)
{
if (!prime[arr[i]])
{
c++;
if (c % k == 0)
{
product *= arr[i];
c = 0;
}
}
}
System.Console.WriteLine(product);
}
static void Main()
{
SieveOfEratosthenes();
int n = 5, k = 2;
int [] arr= new int []{ 2, 3, 5, 7, 11 };
productOfKthPrimes(arr, n, k);
}
}
|
Javascript
<script>
let MAX = 1000000;
let prime = new Array(MAX + 1);
function SieveOfEratosthenes() {
prime.fill( true )
prime[1] = false ;
prime[0] = false ;
for (let p = 2; p * p <= MAX; p++) {
if (prime[p] == true ) {
for (let i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
function productOfKthPrimes(arr, n, k) {
let c = 0;
let product = 1;
for (let i = 0; i < n; i++) {
if (prime[arr[i]]) {
c++;
if (c % k == 0) {
product *= arr[i];
c = 0;
}
}
}
document.write(product + "<br>" );
}
SieveOfEratosthenes();
let n = 5, k = 2;
let arr = [2, 3, 5, 7, 11];
productOfKthPrimes(arr, n, k);
</script>
|
Complexity Analysis:
- Time Complexity : O(n)
- Auxiliary Space: O(MAX)
Last Updated :
07 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...