Magicsheet logo

The Number of Good Subsets

Hard
100%
Updated 6/1/2025

The Number of Good Subsets

What is this problem about?

Combinatorial optimization problems often involve finding subsets that satisfy a "product-based" constraint. In "The Number of Good Subsets," a subset is considered "good" if the product of its elements can be represented as a product of distinct prime numbers. In other words, the product must be square-free. You are given an array of integers (up to 30), and you need to count the number of good subsets, returning the answer modulo 10^9 + 7.

Why is this asked in interviews?

This the Number of Good Subsets interview question is a very hard problem used by companies like Google to test mastery of bitmask dynamic programming and prime factorization. The small range of the input values (1 to 30) is a massive hint that the solution depends on the properties of these specific numbers, particularly the small number of primes under 30. It evaluates whether you can combine mathematical insights with advanced programming techniques.

Algorithmic pattern used

This problem utilizes the Array, Math, Bitmask, Bit Manipulation, Dynamic Programming interview pattern.

  1. Primes under 30: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] (10 primes).
  2. Numbers with square factors (like 4, 8, 9, 12, etc.) can never be part of a good subset.
  3. For each valid number between 2 and 30, represent its prime factorization as a bitmask of the 10 primes.
  4. Use DP with bitmasking: dp[mask] is the number of ways to form a product whose prime factors are represented by mask.
  5. For each number 'i' in the input (count its occurrences), update the DP table: if a new mask doesn't overlap with an existing mask, you can combine them.
  6. Special case: The number 1 can be included in any subset any number of times (2^count_of_1s ways).

Example explanation

Input [2, 3, 6].

  • 2: mask 001. 3: mask 010. 6: mask 011.
  • Initial dp[0] = 1.
  • Add 2: dp[001] = 1.
  • Add 3: dp[010] = 1, dp[011] = 1 (from combining with 2).
  • Add 6: Can't combine with 2 or 3 because masks overlap. Can only combine with dp[0] to add another way to get mask 011.
  • Final good products: mask 001(2), 010(3), 011(6, or 2*3). Total count depends on how many of each number you have.

Common mistakes candidates make

In "The Number of Good Subsets coding problem," a common error is not handling the number 1 correctly; because 1 doesn't change the prime factorization, it can be added to any good subset. Another mistake is forgetting the modulo operation at each step, leading to integer overflow. Finally, many candidates miss the constraint that the product must have distinct primes, meaning numbers like 4 or 9 cannot be used at all.

Interview preparation tip

Bitmask DP is a powerful technique for problems where the state can be represented as a set of small boolean flags (like "is this prime already used?"). Practice identifying these small-range constraints and mapping them to bits. Also, review prime factorization and properties of square-free numbers.

Similar Questions