Introduction
Subsets means:
- generating all possible combinations
- from array elements
Each element has:
- two choices
- include
- exclude
Example:
Input:
1 2 3
Output:
[]
[1]
[2]
[3]
[1,2]
[1,3]
[2,3][1,2,3]
Explanation:
Every element can either:
- participate - or not participatenin subset creation.
This problem is one of the most important applications of:
Backtracking Constraints
1 <= Array Size <= 20Approach : Backtracking Solution
Explanations:
Explanation:
The idea is:
- recursively decide
- whether to include current element
Steps:
- Add current subset to answer.
- Traverse remaining elements.
- Include current element.
- Recurse further.
- Backtrack and remove element.
This approach:
- explores all possibilities
- generates power set
Dry Run
Array:
1 2 3
Start:
[]
Include 1:
[1]
Include 2:
[1,2]
Include 3:
[1,2,3]
Backtrack:
[1,2]
Backtrack:
[1]Continue remaining subsets...
Practice :