Introduction
Combination Problems means:
- selecting elements
- to form valid combinations
Unlike permutations:
- order does NOT matter
Example:
Input:Candidates = [2,3,6,7] Target = 7
Output:
[2,2,3]
[7]
Explanation:
Different combinations can produce the target value. This problem is one of the most important applications of:
BacktrackingConstraints
1 <= Array Size <= 20Approach : Backtracking Solution
Explanations:
Explanation:
The idea is:
- recursively try combinations
- track remaining target
Steps:
- Choose current element.
- Add to combination.
- Reduce target value.
- Recurse further.
- Backtrack and remove element.
Stopping Conditions:
- target becomes 0 → valid answer
- target becomes negative → stop recursion
This approach:
- explores valid combinations
- prunes invalid paths
Dry Run
Candidates:2 3 6 7
Target:
7
Choose 2:
[2]
Choose 2:
[2,2]
Choose 3:
[2,2,3]
Target becomes 0. Valid combination found.
Practice :