Leetcode 2521: Distinct Prime Factors of Product of Array

grid47
grid47
Exploring patterns and algorithms
Feb 28, 2024 6 min read

Given an array of positive integers nums, find the number of distinct prime factors in the product of all the elements in the array. A prime factor of a number is a number that divides it evenly, and only divisible by 1 and itself. The goal is to calculate the distinct prime factors present in the prime factorization of the product of all elements in nums.
Problem
Approach
Steps
Complexity
Input: You are given an array `nums` containing positive integers. You need to calculate the number of distinct prime factors of the product of all elements in `nums`.
Example: nums = [3, 9, 12, 15]
Constraints:
• 1 <= nums.length <= 10^4
• 2 <= nums[i] <= 1000
Output: Return the number of distinct prime factors of the product of all elements in `nums`.
Example: Output: 3
Constraints:
• The output should be an integer representing the number of distinct prime factors.
Goal: The goal is to find how many distinct prime factors are present in the prime factorization of the product of all elements in the `nums` array.
Steps:
• Initialize a set to store prime factors.
• Iterate through each number in the `nums` array.
• For each number, find its prime factors and add them to the set.
• After processing all numbers, the size of the set will give the number of distinct prime factors.
Goal: The array `nums` contains between 1 and 10^4 elements, and each element is between 2 and 1000.
Steps:
• 1 <= nums.length <= 10^4
• 2 <= nums[i] <= 1000
Assumptions:
• All elements in `nums` are positive integers and greater than 1.
Input: nums = [3, 9, 12, 15]
Explanation: The product of these numbers is `4860`. The prime factorization of `4860` is `2^2 * 3^5 * 5`. Hence, there are 3 distinct prime factors: `2`, `3`, and `5`.

Input: nums = [4, 16, 64]
Explanation: The product of these numbers is `4096`, which has the prime factorization `2^12`. Hence, there is only 1 distinct prime factor: `2`.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus