Distinct Prime Factors of Array Product
Last Updated :
24 Oct, 2022
Given an array of integers. Let us say P is the product of elements of the array. Find the number of distinct prime factors of product P.
Examples:
Input : 1 2 3 4 5
Output : 3
Explanation: Here P = 1 * 2 * 3 * 4 * 5 = 120. Distinct prime divisors of 120 are 2, 3 and 5. So, the output is 3.
Input : 21 30 15 24 16
Output : 4
Explanation: Here P = 21 * 30 * 15 * 24 * 16 = 3628800. Distinct prime divisors of 3628800 are 2, 3, 5 and 7. So, the output is 4.
Naive Approach: The simple solution for the problem would be to multiply every number in the array and then find the number of distinct prime factors of the product.
But this method can lead to integer overflow.
Better Approach: To avoid the overflow instead of multiplying the numbers we can find the prime factors of each element separately and store the prime factors in a set or a map for unique factors.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int Distinct_Prime_factors(vector< int > a)
{
unordered_set< int > m;
for ( int i = 0; i < a.size(); i++) {
int sq = sqrt (a[i]);
for ( int j = 2; j <= sq; j++) {
if (a[i] % j == 0) {
m.insert(j);
while (a[i] % j == 0) {
a[i] /= j;
}
}
}
if (a[i] > 1) {
m.insert(a[i]);
}
}
return m.size();
}
int main()
{
vector< int > a = { 1, 2, 3, 4, 5 };
cout << Distinct_Prime_factors(a) << '\n' ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int Distinct_Prime_factors(Vector<Integer> a)
{
HashSet<Integer> m = new HashSet<Integer>();
for ( int i = 0 ; i < a.size(); i++) {
int sq = ( int )Math.sqrt(a.get(i));
for ( int j = 2 ; j <= sq; j++) {
if (a.get(i) % j == 0 ) {
m.add(j);
while (a.get(i) % j == 0 ) {
a.set(i, a.get(i) / j);
}
}
}
if (a.get(i) > 1 ) {
m.add(a.get(i));
}
}
return m.size();
}
public static void main(String args[])
{
Vector<Integer> a = new Vector<Integer>();
a.add( 1 );
a.add( 2 );
a.add( 3 );
a.add( 4 );
a.add( 5 );
System.out.println(Distinct_Prime_factors(a));
}
}
|
Python3
import math
def Distinct_Prime_factors( a):
m = []
for i in range ( len (a)) :
sq = int (math.sqrt(a[i]))
for j in range ( 2 , sq + 1 ) :
if (a[i] % j = = 0 ) :
m.append(j)
while (a[i] % j = = 0 ) :
a[i] / / = j
if (a[i] > 2 ) :
m.append(a[i])
return len (m)
if __name__ = = "__main__" :
a = [ 1 , 2 , 3 , 4 , 5 ]
print (Distinct_Prime_factors(a))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int Distinct_Prime_factors(List< int > a)
{
HashSet< int > m = new HashSet< int >();
for ( int i = 0; i < a.Count; i++) {
int sq = ( int )Math.Sqrt(a[i]);
for ( int j = 2; j <= sq; j++) {
if (a[i] % j == 0) {
m.Add(j);
while (a[i] % j == 0) {
a[i] = a[i] / j;
}
}
}
if (a[i] > 1) {
m.Add(a[i]);
}
}
return m.Count;
}
public static void Main()
{
List< int > a = new List< int >();
a.Add(1);
a.Add(2);
a.Add(3);
a.Add(4);
a.Add(5);
Console.WriteLine(Distinct_Prime_factors(a));
}
}
|
Javascript
<script>
function Distinct_Prime_factors(a)
{
let m = new Set();
for (let i = 0; i < a.length; i++)
{
let sq = Math.floor(Math.sqrt(a[i]));
for (let j = 2; j <= sq; j++)
{
if (a[i] % j == 0)
{
m.add(j);
while (a[i] % j == 0)
{
a[i]= Math.floor(a[i] / j);
}
}
}
if (a[i] > 1)
{
m.add(a[i]);
}
}
return m.size;
}
let a = [ 1, 2, 3, 4, 5 ];
document.write(Distinct_Prime_factors(a));
</script>
|
Time Complexity: O(n*sqrt(n))
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...