Count the Number of Square-Free Subsets - Problem

Given a positive integer array nums, you need to find all square-free subsets and count them.

A subset is square-free if the product of its elements is a square-free integer. A square-free integer is one that isn't divisible by any perfect square greater than 1 (like 4, 9, 16, 25, etc.).

Example: If nums = [3, 4, 4, 5], the subset [3, 5] is square-free because 3 × 5 = 15, and 15 isn't divisible by any perfect square > 1. However, [4, 4] isn't square-free because 4 × 4 = 16 = 4².

Return the count of all non-empty square-free subsets modulo 109 + 7.

Input & Output

example_1.py — Basic Case
$ Input: [3, 4, 4, 5]
Output: 3
💡 Note: Square-free subsets are [3], [5], and [3,5]. The subset [4] has product 4 = 2², [4,4] has product 16 = 4², [3,4] has product 12 = 4×3, and [4,5] has product 20 = 4×5, all containing square factors.
example_2.py — With Ones
$ Input: [1, 2, 3]
Output: 6
💡 Note: All possible non-empty subsets are square-free: [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]. The number 1 can be combined with any other square-free subset.
example_3.py — Only Perfect Squares
$ Input: [4, 9, 16]
Output: 0
💡 Note: All numbers are perfect squares (4=2², 9=3², 16=4²), so no subset can be square-free except the empty set, which is not counted.

Visualization

Tap to expand
Prime Factor Bitmask VisualizationPrime Factors2→bit 03→bit 15→bit 27→bit 3Input Numbers:62×3→011102×5→101153×5→110DP States:dp[000]=1dp[011]=1dp[101]=1dp[110]=1Valid Combinations:[6] ✓[10] ✓[15] ✓[6,10] ✗[6,15] ✗Overlapping bits = not compatibleFinal AnswerCount = 3🎯 Key: Use bitmasks to efficiently check prime factor conflicts
Understanding the Visualization
1
Extract Prime Factors
Convert each number to a bitmask representing its unique prime factors
2
Initialize DP
dp[mask] represents the number of ways to achieve prime factor combination 'mask'
3
Process Each Number
For each valid number, combine it with existing compatible states
4
Handle Special Cases
Process 1's separately as they can combine with any square-free subset
5
Calculate Final Result
Sum all non-empty states and account for subsets containing only 1's
Key Takeaway
🎯 Key Insight: By representing each number as a bitmask of its prime factors, we can use bitwise operations to quickly determine if two numbers can coexist in a square-free subset (no overlapping bits = no repeated prime factors).

Time & Space Complexity

Time Complexity
⏱️
O(n × 2^k)

n elements, 2^k possible prime factor combinations where k ≤ 10

n
2n
Linear Growth
Space Complexity
O(2^k)

DP table storing counts for each prime factor bitmask

n
2n
Linear Space

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000
  • Time limit: 2 seconds
  • Memory limit: 256 MB
Asked in
Google 15 Microsoft 12 Amazon 8 Meta 6
28.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen