LeetCode 1994: The Number of Good Subsets Solution

Master LeetCode problem 1994 (The Number of Good Subsets), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1994. The Number of Good Subsets

Problem Explanation

Explanation

To solve this problem, we can use dynamic programming along with bitmasking. We can iterate through all possible subsets of the input array nums and keep track of the product of each subset. If the product can be represented as a product of distinct prime numbers, then we increment the count of good subsets.

Here's the high-level algorithm:

  1. Generate all prime numbers up to 30 (the maximum value in the input array).
  2. Initialize a frequency array primeFreq to store the frequency of each prime number in the input array.
  3. Initialize a bitmask DP array where each state represents a subset of the input array. The value at each state will be the number of good subsets that can be formed with that subset.
  4. Iterate through all possible subsets using bitmasking and calculate the product of each subset.
  5. For each subset, check if the product can be represented as a product of distinct prime numbers using the primeFreq array.
  6. Update the DP array with the count of good subsets.
  7. Finally, return the total count of good subsets modulo 10^9 + 7.

Time Complexity: O(2^n) where n is the number of elements in the input array. Space Complexity: O(2^n) for the DP array.

Solution Code

class Solution {
    public int numberOfGoodSubsets(int[] nums) {
        int MOD = 1000000007;
        int[] primes = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
        int[] primeFreq = new int[31];
        for (int prime : primes) {
            primeFreq[prime] = 1 << (prime - 1);
        }
        
        int[] dp = new int[1 << 10];
        dp[0] = 1;
        
        for (int num : nums) {
            if (num == 1) continue;
            int mask = 0;
            while (num > 1) {
                int p = 0;
                while (num % primes[p] != 0) {
                    p++;
                }
                num /= primes[p];
                if ((mask & (1 << p)) != 0) {
                    mask = 0;
                    break;
                }
                mask |= 1 << p;
            }
            if (mask == 0) continue;
            for (int i = dp.length - 1; i >= 0; i--) {
                dp[i | mask] += dp[i];
                dp[i | mask] %= MOD;
            }
        }
        
        int result = 0;
        for (int i = 0; i < dp.length; i++) {
            result = (result + dp[i]) % MOD;
        }
        
        return result - 1;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1994 (The Number of Good Subsets)?

This page provides optimized solutions for LeetCode problem 1994 (The Number of Good Subsets) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1994 (The Number of Good Subsets)?

The time complexity for LeetCode 1994 (The Number of Good Subsets) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1994 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1994 in Java, C++, or Python.

Back to LeetCode Solutions