Check if each element of the given array is the product of exactly K prime numbers
Last Updated : 21 Jun, 2022
Given an array of numbers [Tex]A = \{a\textsubscript{1}, a\textsubscript{2} … a\textsubscript{n}\} [/Tex]and the value of [Tex]k [/Tex], check if each number [Tex]a\textsubscript{i} \in A [/Tex]can be expressed as the product of exactly [Tex]k [/Tex]prime numbers. For every element of the array print ‘YES’ if the condition is satisfied, else print ‘NO’.
Note: Repeating prime numbers may also be considered. For example, if k = 2, then n = 4 (=2*2) is a valid input.
Let’s consider the number 36. 36 can be factorised as 2*2*3*3. Hence, it is the product of 4 prime numbers. If k value is 4, then the output should be YES. For other values of k, output should be NO.
More Examples:
Input: arr[] = {30, 8, 12}, K = 3 Output: YES, YES, YES 30 = 2*3*5 8 = 2*2*2 12 = 2*3*2 Input: arr[] = {30, 16, 32}, k = 5 Output: NO, NO, YES Only 32 can be represented as product of 5 prime numbers.
In this article, we shall check if the given number(s) can be expressed as the product of exactly k prime numbers. The underlying concept that we shall we be using is simply a variation of the Sieve of Eratosthenes.
Recommended: How to construct the Sieve of Eratosthenes
Key Difference: In the Sieve, instead of storing binary values (0 if number not prime, 1 if the number is prime), we can store how many (repeating) prime factors make up that number instead. This modification is done during its construction.
The generalised procedure to create this modified sieve is as follows:
- Create an array full of 0’s to store the list of consecutive integers (2, 3, 4 … 10^6).
- Let the value of i be set to 2 initially. This is our first prime number.
- Loop through all the multiples of i (2*i, 3*i … till 10^6) by storing its value as j. Proceed with steps 3 and 4.
- Calculate the number of times j can be factorised using i and store the result into the variable count.
- When the number j cannot be further factorised using i, increment the value of Sieve[j] by the count value.
- Finally, find the next prime number greater than i in the list of integers. If there is no such number then terminate the process. Else, follow from step 2 again.
Explanation with example:
STEP 1:
The initialised empty sieve array looks as illustrated below. For simplicity’s sake let’s only concentrate on indices 2 through 12. The values stored initially is 0 for all indices.
Now, the first prime number that we will take is 2. This is the value of i.

STEP 2:
Initialise variable j to hold the value of every subsequent multiple of i starting from 2*i, which in this case is 4.

STEP 3:
The third step involves the computation of the prime factorisation of j. More specifically, we are just interested in counting the number of occurrences of i when you factorise j.
The computation procedure is simple. Just divide the value of j with i until you get a number that is not divisible by i. Here, 4 can be divided by 2 twice. 4/2 yields 2 and 2/2 yields 1, which is not divisible by 2 and the loop stops. Hence, we update the value of Sieve[4] with the value of the count variable, which is 2.

STEP 4:
We can proceed with the other elements in a similar fashion. Next, the value of j is 6. 6 can only be divided by 2 once. Hence the value of Sieve[6] is 1.


The final computed Sieve array should look something like this. Note that any index storing the value 0 represents a number that is not the product of 2 or more prime numbers. This includes all prime numbers, 0 and 1.
The second thing to note is that we need to only check

Below is the implementation of the above approach:
C++
#include <iostream>
#define MAX 1000000
using namespace std;
int Sieve[MAX] = { 0 };
void constructSieve()
{
for ( int i = 2; i <= MAX; i++) {
if (Sieve[i] == 0) {
for ( int j = 2 * i; j <= MAX; j += i) {
int temp = j;
while (temp > 1 && temp % i == 0) {
Sieve[j]++;
temp = temp / i;
}
}
}
}
}
void checkElements( int A[], int n, int k)
{
for ( int i = 0; i < n; i++) {
if (Sieve[A[i]] == k) {
cout << "YES\n" ;
}
else {
cout << "NO\n" ;
}
}
}
int main()
{
constructSieve();
int k = 3;
int A[] = { 12, 36, 42, 72 };
int n = sizeof (A) / sizeof ( int );
checkElements(A, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAX = 1000000 ;
static int [] Sieve = new int [MAX+ 1 ];
static void constructSieve()
{
for ( int i = 2 ; i <= MAX; i++)
{
if (Sieve[i] == 0 )
{
for ( int j = 2 * i; j <= MAX; j += i)
{
int temp = j;
while (temp > 1 && temp % i == 0 )
{
Sieve[j]++;
temp = temp / i;
}
}
}
}
}
static void checkElements( int A[], int n, int k)
{
for ( int i = 0 ; i < n; i++)
{
if (Sieve[A[i]] == k)
{
System.out.println( "YES" );
}
else
{
System.out.println( "No" );
}
}
}
public static void main(String[] args)
{
constructSieve();
int k = 3 ;
int A[] = { 12 , 36 , 42 , 72 };
int n = A.length;
checkElements(A, n, k);
}
}
|
Python3
MAX = 1000000
Sieve = [ 0 ] * ( MAX + 1 )
def constructSieve() :
for i in range ( 2 , MAX + 1 ) :
if (Sieve[i] = = 0 ) :
for j in range ( 2 * i, MAX + 1 , i) :
temp = j;
while (temp > 1 and temp % i = = 0 ) :
Sieve[j] + = 1 ;
temp = temp / / i;
def checkElements(A, n, k) :
for i in range (n) :
if (Sieve[A[i]] = = k) :
print ( "YES" );
else :
print ( "NO" );
if __name__ = = "__main__" :
constructSieve();
k = 3 ;
A = [ 12 , 36 , 42 , 72 ];
n = len (A);
checkElements(A, n, k);
|
C#
using System;
class GFG
{
static int MAX = 1000000;
static int [] Sieve = new int [MAX+1];
static void constructSieve()
{
for ( int i = 2; i <= MAX; i++)
{
if (Sieve[i] == 0)
{
for ( int j = 2 * i; j <= MAX; j += i)
{
int temp = j;
while (temp > 1 && temp % i == 0)
{
Sieve[j]++;
temp = temp / i;
}
}
}
}
}
static void checkElements( int []A, int n, int k)
{
for ( int i = 0; i < n; i++)
{
if (Sieve[A[i]] == k)
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "No" );
}
}
}
public static void Main()
{
constructSieve();
int k = 3;
int []A = {12, 36, 42, 72};
int n = A.Length;
checkElements(A, n, k);
}
}
|
Javascript
<script>
const MAX = 1000000;
let Sieve = new Array(MAX).fill(0);
function constructSieve()
{
for (let i = 2; i <= MAX; i++) {
if (Sieve[i] == 0) {
for (let j = 2 * i; j <= MAX; j += i)
{
let temp = j;
while (temp > 1 && temp % i == 0)
{
Sieve[j]++;
temp = parseInt(temp / i);
}
}
}
}
}
function checkElements(A, n, k)
{
for (let i = 0; i < n; i++) {
if (Sieve[A[i]] == k) {
document.write( "YES<br>" );
}
else {
document.write( "NO<br>" );
}
}
}
constructSieve();
let k = 3;
let A = [ 12, 36, 42, 72 ];
let n = A.length;
checkElements(A, n, k);
</script>
|
Time Complexity: O(n*log(logn)), where n is the length of the array A.
Space Complexity: O(MAX), where MAX is 106.
Similar Reads
Check if product of array containing prime numbers is a perfect square
Given an array arr[] containing only prime numbers, the task is to check if the product of the array elements is a perfect square or not.Examples: Input: arr[] = {2, 2, 7, 7} Output: Yes 2 * 2 * 7 * 7 = 196 = 142Input: arr[] = {3, 3, 3, 5, 5} Output: No Naive approach: Multiply all the elements of t
4 min read
Check if the given number K is enough to reach the end of an array
Given an array arr[] of n elements and a number K. The task is to determine if it is possible to reach the end of the array by doing the below operations:Traverse the given array and, If any element is found to be non-prime then decrement the value of K by 1.If any element is prime then refill the v
14 min read
Find the array element having equal count of Prime Numbers on its left and right
Given an array arr[] consisting of N positive integers, the task is to find an index from the array having count of prime numbers present on its left and right are equal. Examples: Input: arr[] = {2, 3, 4, 7, 5, 10, 1, 8}Output: 2Explanation: Consider the index 2, then the prime numbers to its left
13 min read
Check if the Product of all Array elements is a Perfect Square or not
Given an array arr[] consisting of N positive integers, the task is to check if the product of all the elements of the given array arr[] is a perfect square or not. If found to be true, then print Yes. Otherwise, print No. Examples: Input: arr[] = {1, 4, 100}Output: YesExplanation: The product of al
6 min read
Sum of array elements which are prime factors of a given number
Given an array arr[] of size N and a positive integer K, the task is to find the sum of all array elements which are prime factors of K. Examples: Input: arr[] = {1, 2, 3, 5, 6, 7, 15}, K = 35Output: 12Explanation: From the given array, 5 and 7 are prime factors of 35. Therefore, required sum = 5 +
8 min read
Check whether the sum of prime elements of the array is prime or not
Given an array having N elements. The task is to check if the sum of prime elements of the array is prime or not. Examples: Input: arr[] = {1, 2, 3} Output: Yes As there are two primes in the array i.e. 2 and 3. So, the sum of prime is 2 + 3 = 5 and 5 is also prime. Input: arr[] = {2, 3, 2, 2} Outpu
10 min read
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
12 min read
Numbers less than N which are product of exactly two distinct prime numbers
Given a number [Tex]N [/Tex]. The task is to find all such numbers less than N and are a product of exactly two distinct prime numbers. For Example, 33 is the product of two distinct primes i.e 11 * 3, whereas numbers like 60 which has three distinct prime factors i.e 2 * 2 * 3 * 5.Note: These numbe
8 min read
Product of all the elements in an array divisible by a given number K
Given an array containing N elements and a number K. The task is to find the product of all such elements of the array which are divisible by K. Examples: Input : arr[] = {15, 16, 10, 9, 6, 7, 17} K = 3 Output : 810 Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9} K = 2 Output : 384 The idea is to traverse
6 min read
Count the number of primes in the prefix sum array of the given array
Given an array arr[] of N integers, the task is to count the number of primes in the prefix sum array of the given array. Examples: Input: arr[] = {1, 4, 8, 4} Output: 3 The prefix sum array is {1, 5, 13, 17} and the three primes are 5, 13 and 17. Input: arr[] = {1, 5, 2, 3, 7, 9} Output: 1 Approach
9 min read