Generate All Subsets Powerset in DSA C - Time & Space Complexity
When we generate all subsets of a set, we want to know how the work grows as the set gets bigger.
We ask: How many steps does it take to list every possible subset?
Analyze the time complexity of the following code snippet.
void generateSubsets(int arr[], int n) {
int total = 1 << n; // 2^n subsets
for (int i = 0; i < total; i++) {
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
// include arr[j] in current subset
}
}
}
}
This code creates every subset by checking each combination of elements using bits.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops where outer runs 2^n times and inner runs n times.
- How many times: Outer loop runs once for each subset (2^n), inner loop runs n times per subset.
As the input size n grows, the number of subsets doubles each time, and for each subset, we check n elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~10 * 2^10 = 10,240 |
| 20 | ~20 * 2^20 = 20,971,520 |
| 30 | ~30 * 2^30 = 32,212,254,720 |
Pattern observation: The work grows very fast, doubling with each added element and multiplying by n.
Time Complexity: O(n * 2^n)
This means the time needed grows very fast as the input size grows, because we list all subsets and check each element.
[X] Wrong: "Generating all subsets is just O(2^n) because there are 2^n subsets."
[OK] Correct: We also spend time checking each element in every subset, so the work multiplies by n, making it O(n * 2^n).
Understanding this complexity helps you explain why generating all subsets is costly and shows you can analyze nested loops with exponential growth.
"What if we only wanted subsets of a fixed size k? How would the time complexity change?"
Practice
powerset mean in the context of subsets?Solution
Step 1: Understand the definition of powerset
The powerset of a set is the collection of all possible subsets, including the empty set and the set itself.Step 2: Compare options with definition
All possible subsets of a given set including empty and full set correctly describes all subsets including empty and full set. Other options limit subsets incorrectly.Final Answer:
All possible subsets of a given set including empty and full set -> Option BQuick Check:
powerset = all subsets [OK]
- Thinking powerset excludes empty set
- Confusing powerset with only even-sized subsets
- Assuming powerset means largest subset only
Solution
Step 1: Understand empty subset initialization in C
An empty subset list for powerset starts with one subset: the empty set, which has zero elements.Step 2: Check array dimensions and initialization
int subsets[1][0] = {{}}; correctly declares an array with 1 subset of size 0, initialized empty. Other options have invalid sizes or syntax.Final Answer:
int subsets[1][0] = {{}}; -> Option CQuick Check:
Empty subset list = subsets[1][0] [OK]
- Using zero subsets instead of one
- Incorrect array size for empty subset
- Invalid initialization syntax in C
arr = {1, 2}?
#include
void printSubsets(int arr[], int n) {
int total = 1 << n;
for (int i = 0; i < total; i++) {
printf("{");
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
printf("%d ", arr[j]);
}
}
printf("} ");
}
}
int main() {
int arr[] = {1, 2};
printSubsets(arr, 2);
return 0;
}Solution
Step 1: Understand bitmask iteration for subsets
For n=2, total subsets = 4 (0 to 3). Each bit in i represents inclusion of arr[j].Step 2: Trace output for each i
i=0: no bits set -> {}
i=1: bit0 set -> {1 }
i=2: bit1 set -> {2 }
i=3: bits0 and 1 set -> {1 2 }Final Answer:
{} {1} {2} {1 2} -> Option AQuick Check:
Bitmask subsets = {} {1} {2} {1 2} [OK]
- Mixing order of elements in subsets
- Printing subsets without spaces
- Confusing bit positions with element order
arr = {1, 2, 3}:
void generateSubsets(int arr[], int n) {
int total = 1 << n;
for (int i = 0; i <= total; i++) {
printf("{");
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
printf("%d ", arr[j]);
}
}
printf("}\n");
}
}Solution
Step 1: Analyze loop range for total subsets
Total subsets for n elements is 2^n, so loop should run from 0 to total-1.Step 2: Identify off-by-one error
Loop runs from i=0 to i=total (inclusive), which is one extra iteration causing invalid subset index.Final Answer:
Loop runs one extra time causing out-of-range subsets -> Option DQuick Check:
Loop limit = total-1, not total [OK]
- Using <= total instead of < total in loop
- Incorrect bitwise operations
- Wrong inner loop range
arr = {1, 2, 2}, which of the following C code modifications correctly generates all unique subsets (powerset without duplicates)?Solution
Step 1: Understand duplicate subsets problem
Array with duplicates can produce repeated subsets if not handled.Step 2: Use sorting and skip duplicates approach
Sorting arr groups duplicates together. While generating subsets, skip adding duplicate elements at the same recursion level to avoid repeated subsets.Final Answer:
Sort array first, then skip duplicates while generating subsets -> Option AQuick Check:
Sort + skip duplicates = unique subsets [OK]
- Ignoring duplicates causing repeated subsets
- Using hash sets without proper subset hashing
- Limiting subset sizes incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
