Introduction
The Subsets problem (LeetCode 78) is one of the most fundamental and frequently asked questions in coding interviews. It introduces the concept of generating a power set, which is a core idea in recursion, backtracking, and combinatorics.
Mastering this problem helps in solving a wide range of advanced problems like combinations, permutations, and decision-based recursion.
In this article, we will explore:
- Intuition behind subsets
- Recursive (backtracking) approach
- Iterative (loop-based) approach
- Bit manipulation approach
- Time and space complexity analysis
Problem Statement
Given an integer array nums of unique elements, return all possible subsets (the power set).
Key Points
- Each element can either be included or excluded
- No duplicate subsets
- Return subsets in any order
Examples
Example 1
Input:
nums = [1, 2, 3]
Output:
[[], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]]
Example 2
Input:
nums = [0]
Output:
[[], [0]]
Key Insight
For each element, there are two choices:
So total subsets:
This makes it a binary decision tree problem, very similar to:
- Permutation with Spaces
- Binary choices recursion
- Backtracking problems
Approach 1: Recursion + Backtracking (Most Important)
Intuition
At each index:
- Skip the element
- Include the element
Build subsets step by step and backtrack.
Java Code (With Explanation)
Dry Run (nums = [1,2])
Final Output:
Approach 2: Iterative (Loop-Based)
Intuition
Start with an empty subset:
For each element:
- Add it to all existing subsets
Code
How It Works
For [1,2,3]:
Approach 3: Bit Manipulation
Intuition
Each subset can be represented using a binary number:
For n = 3:
Code
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
| Recursion | O(2^n) | O(n) stack |
| Iterative | O(2^n) | O(2^n) |
| Bit Manipulation | O(2^n) | O(2^n) |
Why All Approaches Are O(2ⁿ)
Because:
- Total subsets = 2ⁿ
- Each subset takes up to O(n) to construct
When to Use Which Approach
- Recursion / Backtracking → Best for interviews (easy to explain)
- Iterative → Clean and beginner-friendly
- Bit Manipulation → Best for optimization & advanced understanding
Key Takeaways
- Subsets = power set problem
- Every element → 2 choices
- Think in terms of decision trees
- Backtracking = build + undo (add/remove)
Common Interview Variations
- Subsets with duplicates
- Combination sum
- Permutations
- K-sized subsets
Conclusion
The Subsets problem is a foundational DSA concept that appears across many interview questions. Understanding all approaches—especially recursion and iterative expansion—gives a strong base for solving complex backtracking problems.
If you master this pattern, you unlock a whole category of problems in recursion and combinatorics.
Frequently Asked Questions (FAQs)
1. Why are there 2ⁿ subsets?
Because each element has 2 choices: include or exclude.
2. Which approach is best for interviews?
Recursion + backtracking is the most preferred.
3. Is bit manipulation important?
Yes, it helps in optimizing and understanding binary patterns.




