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.
- Sort an array according to the increasing count of distinct Prime Factors
- Number of distinct prime factors of first n natural numbers
- Product of unique prime factors of a number
- Find two distinct prime numbers with given product
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Number which has the maximum number of distinct prime factors in the range M to N
- Prime factors of LCM of array elements
- Sum of element whose prime factors are present in array
- Product of non-repeating (distinct) elements in an Array
- Numbers less than N which are product of exactly two distinct prime numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Product of every K’th prime number in an array
- Product of all prime numbers in an Array
- Product of elements in an array having prime frequency
- Sum and product of k smallest and k largest prime numbers in the 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.