How to find all possible subsets of a given array?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In the realm of computer science and discrete mathematics, finding all possible subsets of a given array is an interesting problem with numerous applications. Subsets are foundational in problems related to combinatorics, power sets, and even computational complexity. Here, we delve into methods to find all possible subsets of an array, including comprehensive technical explanations and examples.
Understanding Subsets
A subset of a set is any combination of elements that can be derived from the original set. This includes the empty set and the set itself. If a set has `n` elements, the number of possible subsets is , as each element can either be included in a subset or not.
Example
Consider the array: `[1, 2, 3]`. Its subsets include:
- `[]` (Empty set)
- `[1]`
- `[2]`
- `[3]`
- `[1, 2]`
- `[1, 3]`
- `[2, 3]`
- `[1, 2, 3]`
Technical Approaches to Generate Subsets
1. Iterative Approach Using Bit Manipulation
An efficient way to generate subsets is by using bit manipulation. This is possible because each subset corresponds to a distinct binary number with `n` bits, where `n` is the number of elements in the array.
Explanation: Each bit represents whether the corresponding element is included in the subset. For example, in a 3-bit system, `101` means include the first and third elements.
Algorithm:
- Iterate over numbers from `0` to .
- For each number, use binary representation to decide if an element should be included in the subset.
Implementation in Python:
- Start with an empty subset.
- At each step, consider two possibilities for each element: include it or exclude it.
- Recursively build subsets for remaining elements.
- Start with a queue containing the empty subset.
- For each element in the array, generate new subsets by adding the element to existing subsets in the queue.
- Combinatorial Optimization: Solving problems like the Knapsack problem.
- Set Theory: Exploring powersets and set operations.
- Data Mining: Generating frequent itemsets in algorithms like Apriori.
Related reading
- How to find all taxicab numbers less than N?
- How to find all vertex-disjoint paths in a graph?
- How to find common strings among two very large files?
- How to find convex hull in a 3 dimensional space
- How to find cycles of a given length in a directed graph? Using networkx
- How to find if there are n consecutive set bits in a 32 bit buffer?
- How to find if the second hand of a clock lies in the larger area or smaller one
- How to find integer nth roots?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.