You are given an integer array nums. We call a subset of nums good if its product can be represented as a product of one or more distinct prime numbers.
nums = [1, 2, 3, 4]:
[2, 3], [1, 2, 3], and [1, 3] are good subsets with products 6 = 2*3, 6 = 2*3, and 3 = 3 respectively.[1, 4] and [4] are not good subsets with products 4 = 2*2 and 4 = 2*2 respectively.Return the number of different good subsets in nums modulo 109 + 7.
A subset of nums is any array that can be obtained by deleting some (possibly none or all) elements from nums. Two subsets are different if and only if the chosen indices to delete are different.
Example 1:
Input: nums = [1,2,3,4] Output: 6 Explanation: The good subsets are: - [1,2]: product is 2, which is the product of distinct prime 2. - [1,2,3]: product is 6, which is the product of distinct primes 2 and 3. - [1,3]: product is 3, which is the product of distinct prime 3. - [2]: product is 2, which is the product of distinct prime 2. - [2,3]: product is 6, which is the product of distinct primes 2 and 3. - [3]: product is 3, which is the product of distinct prime 3.
Example 2:
Input: nums = [4,2,3,15] Output: 5 Explanation: The good subsets are: - [2]: product is 2, which is the product of distinct prime 2. - [2,3]: product is 6, which is the product of distinct primes 2 and 3. - [2,15]: product is 30, which is the product of distinct primes 2, 3, and 5. - [3]: product is 3, which is the product of distinct prime 3. - [15]: product is 15, which is the product of distinct primes 3 and 5.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 30When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of numbers and want to find every possible subgroup where multiplying the chosen numbers together does not result in any number being used more than once as a prime building block. The brute force approach involves exploring every single combination of the given numbers, checking if each combination meets this special multiplication rule, and counting how many valid combinations you discover.
Here's how the algorithm would work step-by-step:
from itertools import combinations
def numberOfGoodSubsets(number_collection):
winning_combination_tally = 0
# We evaluate every subset size to ensure all mathematical combinations are thoroughly explored
for subset_size in range(1, len(number_collection) + 1):
for current_combination in combinations(number_collection, subset_size):
product_value = 1
for current_number in current_combination:
product_value *= current_number
prime_factor_counts = {}
temporary_product = product_value
current_divisor = 2
while current_divisor * current_divisor <= temporary_product:
while temporary_product % current_divisor == 0:
prime_factor_counts[current_divisor] = prime_factor_counts.get(current_divisor, 0) + 1
temporary_product //= current_divisor
current_divisor += 1
if temporary_product > 1:
prime_factor_counts[temporary_product] = prime_factor_counts.get(temporary_product, 0) + 1
# We check if any prime factor appears more than once to enforce square-free constraint
is_square_free = True
for factor_count in prime_factor_counts.values():
if factor_count > 1:
is_square_free = False
break
# We increment the tally only if the subset product is valid and square-free
if is_square_free:
winning_combination_tally += 1
return winning_combination_tallyTo find the number of good groups, we can take advantage of the fact that only a small set of prime numbers matter and numbers with repeated factors are useless. By counting how many times each useful number appears and tracking which prime building blocks have already been used, we can efficiently build valid combinations without checking every single possibility.
Here's how the algorithm would work step-by-step:
class Solution:
def numberOfGoodSubsets(self, numbers_list: list[int]) -> int:
prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
modulo_constant = 10**9 + 7
frequency_map = {}
for current_number in numbers_list:
frequency_map[current_number] = frequency_map.get(current_number, 0) + 1
# Filter out numbers with repeated prime factors since they invalidate subsets.
valid_mask_counts = {0: 1}
for current_number in range(2, 31):
if current_number not in frequency_map:
continue
is_square_free = True
prime_mask = 0
for prime_index, prime_value in enumerate(prime_numbers):
prime_divisibility_count = 0
while current_number % prime_value == 0:
prime_divisibility_count += 1
current_number //= prime_value
if prime_divisibility_count > 1:
is_square_free = False
break
if prime_divisibility_count == 1:
prime_mask |= (1 << prime_index)
if not is_square_free:
continue
# Update subset combinations using bitmasks to track used prime factors.
current_number_frequency = frequency_map[current_number]
for existing_mask in list(valid_mask_counts.keys():
if (existing_mask & prime_mask) == 0:
new_combined_mask = existing_mask | prime_mask
added_ways = (valid_mask_counts[existing_mask] * current_number_frequency) % modulo_constant
valid_mask_counts[new_combined_mask] = (valid_mask_counts.get(new_combined_mask, 0) + added_ways) % modulo_constant
total_good_subsets = sum(count for mask, count in valid_mask_counts.items() if mask > 0) % modulo_constant
# Multiply by powers of two for ones since each one can be included or excluded independently.
if 1 in frequency_map:
power_of_ones = pow(2, frequency_map[1], modulo_constant)
total_good_subsets = (total_good_subsets * power_of_ones) % modulo_constant
return total_good_subsets| Case | How to Handle |
|---|---|
| Array contains the number 1 multiple times | Each occurrence of 1 can be included or excluded independently, meaning 1s contribute a factor of 2 to the power of the count of 1s in nums. |
| Array contains numbers with squared prime factors like 4, 8, or 9 | These numbers must be completely ignored during subset formation because their prime factorization contains duplicate primes. |
| Array contains only numbers that are not valid prime products | The algorithm should correctly return zero since no valid good subsets can be formed from the input. |
| Input contains the maximum allowed number of elements up to the constraints | Using frequency counting of unique numbers instead of dynamic programming over the entire array ensures the solution executes well within time limits. |
| Subsets can be formed using numbers that share prime factors | Bitmask dynamic programming tracks used prime factors to ensure no prime factor is used more than once within the same subset. |
| Large counts of combinations exceeding standard integer limits | All intermediate calculations and final results must be taken modulo 10 to the 9 plus 7 to prevent integer overflow. |
| Array contains only a single prime number | The algorithm counts this single element as a valid subset of size one and returns the correct total count. |