Count Subsets With Product Equal to a Power of Two
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Tests whether candidates can combine combinatorics (counting subsets) with the insight that a product is a power of two iff every element is itself a power of two.
Count Subsets With Product Equal to a Power of Two
You are given a list of positive integers nums and a non-negative integer k.
A valid subset is any non-empty subset of nums whose product is exactly 2^k (two to the power of k).
Return the number of valid subsets.
Background
Recall that a positive integer is a power of two if and only if it has exactly one set bit in its binary representation (i.e. x & (x - 1) == 0). A product of integers is a power of two if and only if every factor is itself a power of two.
Therefore the problem reduces to:
- Identify which elements of
numsare powers of two. - Among those elements, count non-empty subsets whose exponents (where element
2^econtributes exponente) sum to exactlyk.
Task
Implement:
def count_power_subsets(nums: list[int], k: int) -> int:
numsmay contain elements that are not powers of two; such elements can never appear in a valid subset.- Multiple elements with the same value are treated as distinct (they occupy different positions).
- Return
0if no valid subset exists.
Constraints
1 ≤ len(nums) ≤ 2 0001 ≤ nums[i] ≤ 2^200 ≤ k ≤ 40- The answer fits in a 64-bit signed integer.
Examples
Example 1
Input: count_power_subsets([1, 2, 4, 3], 2)
Output: 2
Example 2
Input: count_power_subsets([4, 2, 2], 2)
Output: 2
Example 3
Input: count_power_subsets([3, 5, 7], 3)
Output: 0
Hints
Hint 1
Filter out elements that are not powers of two first — they can never contribute to a valid subset.
Hint 2
For each remaining element 2^e, extract its exponent e using bit_length() - 1 or int.bit_length(). The problem then becomes: count subsets of a multiset of small non-negative integers that sum to k.
Hint 3
Use a 1-D DP array dp[s] = number of subsets of processed elements whose exponents sum to s, updating it like a 0-1 knapsack (iterate s from k down to 0).
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
A product of positive integers equals 2^k if and only if:
- Every factor in the subset is itself a power of two (otherwise the product gains an odd prime factor and can never be a power of two), and
- The exponents of those powers of two sum to exactly
k.
So we can immediately discard any element that is not a power of two, and replace each surviving element 2^e with its exponent e.
Algorithm
After filtering, the problem becomes: count non-empty subsets of a list of small non-negative integers that sum to exactly k.
This is a classic 0-1 knapsack counting problem:
- Maintain a DP array
dp[0..k]wheredp[s]= number of subsets (including the empty set) of the elements processed so far whose values sum tos. - Initialise
dp[0] = 1(the empty subset), all others0. - For each exponent
e, iteratesfromkdown toeand setdp[s] += dp[s - e]. - After processing all exponents,
dp[k]is the answer — but it includes the empty subset whenk == 0. Subtract 1 in that case.
Correctness
The downward iteration guarantees each element is used at most once (0-1 knapsack). The empty-subset correction handles the edge case k = 0 (the only valid subset must be literally empty products, which the problem excludes).
Complexity
- Time: O(n · k) where n =
len(nums)and k ≤ 40 — at most 2 000 × 40 = 80 000 operations. - Space: O(k) for the DP array.
Reference Solution
from typing import List
def count_power_subsets(nums: List[int], k: int) -> int:
# Step 1: extract exponents of elements that are powers of two
exponents = []
for x in nums:
if x >= 1 and (x & (x - 1)) == 0: # power of two check
exponents.append(x.bit_length() - 1) # 2^e -> e
# Step 2: 0-1 knapsack counting subsets with exponent-sum == k
# dp[s] = number of subsets (possibly empty) of processed elements
# whose exponents sum to exactly s
dp = [0] * (k + 1)
dp[0] = 1 # the empty subset
for e in exponents:
if e <= k:
# iterate high -> low to avoid reusing the same element
for s in range(k, e - 1, -1):
dp[s] += dp[s - e]
# dp[k] counts the empty subset when k==0; subtract it
return dp[k] - (1 if k == 0 else 0)