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.
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 an 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.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Distinct Prime Factors of an Array
- Sort an array according to the increasing count of distinct Prime Factors
- Number of distinct prime factors of first n natural numbers
- Number which has the maximum number of distinct prime factors in the range M to N
- Maximum distinct prime factors of elements in a K-length subarray
- Count ways to split N! into two distinct co-prime factors
- Count numbers in a given range whose count of prime factors is a Prime Number
- Product of unique prime factors of a number
- Find the row whose product has maximum count of prime factors
- Number less than equals to N with maximum product of prime factors
- Product of divisors of a number from a given list of its prime factors
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Numbers less than N which are product of exactly two distinct prime numbers
- Find two distinct prime numbers with given product
- Count pairs whose product contains single distinct prime factor
- Prime factors of LCM of array elements
- Sum of element whose prime factors are present in 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 firstname.lastname@example.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.