2521. Distinct Prime Factors of Product of Array
Given an array of positive integers nums, return the number of distinct prime factors in the product of the elements of nums.
Note that:
- A number greater than
1is called prime if it is divisible by only1and itself. - An integer
val1is a factor of another integerval2ifval2 / val1is an integer.
Example 1:
Input: nums = [2,4,3,7,10,6] Output: 4 Explanation: The product of all the elements in nums is: 2 * 4 * 3 * 7 * 10 * 6 = 10080 = 25 * 32 * 5 * 7. There are 4 distinct prime factors so we return 4.
Example 2:
Input: nums = [2,4,8,16] Output: 1 Explanation: The product of all the elements in nums is: 2 * 4 * 8 * 16 = 1024 = 210. There is 1 distinct prime factor so we return 1.
Constraints:
1 <= nums.length <= 1042 <= nums[i] <= 1000
ArrayHash TableMathNumber TheoryPrime FactorizationEuclidean AlgorithmPrimality TestGreatest Common DivisorSieve TheoryPrime Number Sieve
Hints
Hint 1
Do not multiply all the numbers together, as the product is too big to store.
Hint 2
Think about how each individual number's prime factors contribute to the prime factors of the product of the entire array.
Hint 3
Find the prime factors of each element in nums, and store all of them in a set to avoid duplicates.
Preparing judge…