Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an array of positive integers nums, return the number of distinct prime factors in the product of the elements of nums.
Note that:
1 is called prime if it is divisible by only 1 and itself.val1 is a factor of another integer val2 if val2 / val1 is 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] <= 1000Problem summary: 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 1 is called prime if it is divisible by only 1 and itself. An integer val1 is a factor of another integer val2 if val2 / val1 is an integer.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[2,4,3,7,10,6]
[2,4,8,16]
2-keys-keyboard)largest-component-size-by-common-factor)closest-divisors)smallest-value-after-replacing-with-sum-of-prime-factors)count-the-number-of-square-free-subsets)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2521: Distinct Prime Factors of Product of Array
class Solution {
public int distinctPrimeFactors(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int n : nums) {
for (int i = 2; i <= n / i; ++i) {
if (n % i == 0) {
s.add(i);
while (n % i == 0) {
n /= i;
}
}
}
if (n > 1) {
s.add(n);
}
}
return s.size();
}
}
// Accepted solution for LeetCode #2521: Distinct Prime Factors of Product of Array
func distinctPrimeFactors(nums []int) int {
s := map[int]bool{}
for _, n := range nums {
for i := 2; i <= n/i; i++ {
if n%i == 0 {
s[i] = true
for n%i == 0 {
n /= i
}
}
}
if n > 1 {
s[n] = true
}
}
return len(s)
}
# Accepted solution for LeetCode #2521: Distinct Prime Factors of Product of Array
class Solution:
def distinctPrimeFactors(self, nums: List[int]) -> int:
s = set()
for n in nums:
i = 2
while i <= n // i:
if n % i == 0:
s.add(i)
while n % i == 0:
n //= i
i += 1
if n > 1:
s.add(n)
return len(s)
// Accepted solution for LeetCode #2521: Distinct Prime Factors of Product of Array
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2521: Distinct Prime Factors of Product of Array
// class Solution {
// public int distinctPrimeFactors(int[] nums) {
// Set<Integer> s = new HashSet<>();
// for (int n : nums) {
// for (int i = 2; i <= n / i; ++i) {
// if (n % i == 0) {
// s.add(i);
// while (n % i == 0) {
// n /= i;
// }
// }
// }
// if (n > 1) {
// s.add(n);
// }
// }
// return s.size();
// }
// }
// Accepted solution for LeetCode #2521: Distinct Prime Factors of Product of Array
function distinctPrimeFactors(nums: number[]): number {
const s: Set<number> = new Set();
for (let n of nums) {
let i = 2;
while (i <= n / i) {
if (n % i === 0) {
s.add(i);
while (n % i === 0) {
n = Math.floor(n / i);
}
}
++i;
}
if (n > 1) {
s.add(n);
}
}
return s.size;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.