Product of Primes of all Subsets
Last Updated :
18 May, 2021
Given an array a[] of size N. The value of a subset is the product of primes in that subset. A non-prime is considered to be 1 while finding value by-product. The task is to find the product of the value of all possible subsets.
Examples:
Input: a[] = {3, 7}
Output: 20
The subsets are: {3} {7} {3, 7}
{3, 7} = 3 * 7 = 21
{3} = 3
{7} = 7
21 * 3 * 7 = 441
Input: a[] = {10, 2, 14, 3}
Output: 1679616
Naive Approach: A naive approach is to find all the subsets using power set and then find the product by multiplying all the values of the subset. Prime can be checked using Sieve.
Time Complexity: O(2N)
Efficient Approach: An efficient approach is to solve the problem using observation. If we write all the subsequences, a common point of observation is that each number appears 2(N – 1) times in a subset and hence will lead to the 2(N-1) as the contribution to the product. Iterate through the array and check if the element in the array is prime or not. If it prime, then its contribution is arr[i]2(N-1) times to the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sieve( int n, vector< bool >& prime)
{
for ( int i = 2; i <= n; i++)
prime[i] = true ;
prime[0] = prime[1] = false ;
for ( int i = 2; i <= n; i++) {
if (prime[i]) {
for ( int j = i * i; j <= n; j += i) {
prime[j] = false ;
}
}
}
}
int sumOfSubset( int a[], int n)
{
int maxi = *max_element(a, a + n);
vector< bool > prime(maxi + 1);
sieve(maxi, prime);
int times = pow (2, n - 1);
int sum = 1;
for ( int i = 0; i < n; i++) {
if (prime[a[i]])
sum = sum * ( pow (a[i], times));
}
return sum;
}
int main()
{
int a[] = { 3, 7 };
int n = sizeof (a) / sizeof (a[0]);
cout << sumOfSubset(a, n);
}
|
Java
import java.util.*;
class GFG
{
static void sieve( int n, boolean []prime)
{
for ( int i = 2 ; i <= n; i++)
prime[i] = true ;
prime[ 0 ] = prime[ 1 ] = false ;
for ( int i = 2 ; i <= n; i++)
{
if (prime[i])
{
for ( int j = i * i; j <= n; j += i)
{
prime[j] = false ;
}
}
}
}
static int sumOfSubset( int a[], int n)
{
int maxi = Arrays.stream(a).max().getAsInt();
boolean []prime = new boolean [maxi + 1 ];
sieve(maxi, prime);
int times = ( int ) Math.pow( 2 , n - 1 );
int sum = 1 ;
for ( int i = 0 ; i < n; i++)
{
if (prime[a[i]])
sum = ( int ) (sum * (Math.pow(a[i], times)));
}
return sum;
}
public static void main(String[] args)
{
int a[] = { 3 , 7 };
int n = a.length;
System.out.println(sumOfSubset(a, n));
}
}
|
Python3
prime = [ True for i in range ( 100 )]
def sieve(n, prime):
for i in range ( 1 , n + 1 ):
prime[i] = True
prime[ 0 ] = prime[ 1 ] = False
for i in range ( 2 , n + 1 ):
if (prime[i]):
for j in range ( 2 * i, n + 1 , i):
prime[j] = False
def SumOfSubset(a, n):
maxi = max (a)
sieve(maxi, prime)
times = pow ( 2 , n - 1 )
Sum = 1
for i in range (n):
if (prime[a[i]]):
Sum = Sum * ( pow (a[i], times))
return Sum
a = [ 3 , 7 ]
n = len (a)
print (SumOfSubset(a, n))
|
C#
using System;
using System.Linq;
class GFG
{
static void sieve( int n, Boolean []prime)
{
for ( int i = 2; i <= n; i++)
prime[i] = true ;
prime[0] = prime[1] = false ;
for ( int i = 2; i <= n; i++)
{
if (prime[i])
{
for ( int j = i * i; j <= n; j += i)
{
prime[j] = false ;
}
}
}
}
static int sumOfSubset( int []a, int n)
{
int maxi = a.Max();
Boolean []prime = new Boolean[maxi + 1];
sieve(maxi, prime);
int times = ( int ) Math.Pow(2, n - 1);
int sum = 1;
for ( int i = 0; i < n; i++)
{
if (prime[a[i]])
sum = ( int ) (sum * (Math.Pow(a[i], times)));
}
return sum;
}
public static void Main(String[] args)
{
int []a = { 3, 7 };
int n = a.Length;
Console.WriteLine(sumOfSubset(a, n));
}
}
|
Javascript
<script>
function sieve(n, prime)
{
for ( var i = 2; i <= n; i++)
prime[i] = true ;
prime[0] = prime[1] = false ;
for (i = 2; i <= n; i++)
{
if (prime[i])
{
for (j = i * i; j <= n; j += i) {
prime[j] = false ;
}
}
}
}
function sumOfSubset(a , n) {
var maxi = Math.max.apply(Math,a);
var prime =Array(maxi + 1).fill(0);
sieve(maxi, prime);
var times = parseInt( Math.pow(2, n - 1));
var sum = 1;
for (i = 0; i < n; i++) {
if (prime[a[i]])
sum = parseInt( (sum * (Math.pow(a[i], times))));
}
return sum;
}
var a = [ 3, 7 ];
var n = a.length;
document.write(sumOfSubset(a, n));
</script>
|
Time Complexity: O(M log M) for pre calculation where M is the maximum element and O(N) for iteration.
Space Complexity: O(M)
Note: As arr[i]2(N-1) can be really big, the answer can overflow, its preferable to use larger data-type and mod operations to conserve the answer.
Share your thoughts in the comments
Please Login to comment...