Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count the Number of Square-Free Subsets

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2572" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a positive integer 0-indexed array nums. A subset of the array nums is square-free if the product of its elements is a square-free integer. A square-free integer is an integer that is divisible by no square number other than 1. Return the number of square-free non-empty subsets of the array nums. Since the answer may be too large, return it modulo 109 + 7. A non-empty subset of nums is an array that can be obtained by deleting some (possibly none but not all) elements from nums. Two subsets are different if and only if the chosen indices to delete are different. Example 1: Input: nums = [3,4,4,5] Output: 3 Explanation: There are 3 square-free subsets in this example: - The subset consisting of the 0th element [3]. The product of its elements is 3, which is a square-free integer. - The subset consisting of the 3rd element [5]. The product of its elements is 5, which is a square-free integer. - The subset consisting of 0th and 3rd elements [3,5]. The product of its elements is 15, which is a square-free integer. It can be proven that there are no more than 3 square-free subsets in the given array. Example 2: Input: nums = [1] Output: 1 Explanation: There is 1 square-free subset in this example: - The subset consisting of the 0th element [1]. The product of its elements is 1, which is a square-free integer. It can be proven that there is no more than 1 square-free subset in the given array. Constraints: 1 <= nums.length <= 1000 1 <= nums[i] <= 30

Explanation

  • Prime Factor Masking: Represent each number with a bitmask indicating the presence of prime factors (2, 3, 5, 7, 11, 13, 17, 19, 23, 29). If a number has a squared prime factor, discard it.
    • Dynamic Programming: Use dynamic programming to count square-free subsets. dp[i][mask] stores the number of square-free subsets using the first i numbers with a product mask of mask.
    • Modulo Arithmetic: Apply modulo arithmetic to prevent integer overflow.
  • Runtime Complexity: O(n 210), where n is the length of nums. *Storage Complexity: O(210)

Code

    def squareFreeSubsets(nums):
    MOD = 10**9 + 7
    primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    prime_mask = [0] * 31

    for i in range(2, 31):
        num = i
        mask = 0
        square_free = True
        for j, prime in enumerate(primes):
            if num % prime == 0:
                if num % (prime * prime) == 0:
                    square_free = False
                    break
                mask |= (1 << j)
        if square_free:
            prime_mask[i] = mask

    dp = {}

    def solve(index, mask):
        if index == len(nums):
            return 1 if mask > 0 else 0

        if (index, mask) in dp:
            return dp[(index, mask)]

        # Option 1: Exclude the current number
        ans = solve(index + 1, mask)

        # Option 2: Include the current number if it's square-free and doesn't conflict with the current mask
        num = nums[index]
        if prime_mask[num] != 0 and (mask & prime_mask[num]) == 0:
            ans = (ans + solve(index + 1, mask | prime_mask[num])) % MOD

        dp[(index, mask)] = ans
        return ans

    return (solve(0, 0) - 1 + MOD) % MOD

More from this blog

C

Chatmagic blog

2894 posts