#2572
Count the Number of Square-Free Subsets
expert · 1165 · lc medium +32 · verified · 26.5% accepted · 505 likes · top 5%
Description
Given a positive integer array nums, count the non-empty subsets whose product is a square-free integer (not divisible by any perfect square other than 1). Return the count modulo 109 + 7. Two subsets are considered different if they differ in at least one chosen index.
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.
Code
1
2
3