Approach Summary
Sort candidates, then backtrack with a start index. Skip duplicates at the same recursion level so each element is used once.
Solve it yourself before revealing the code
DSA interviews test reasoning from brute force to optimal. Think through the constraints first.
Solution Progression (Brute Force → Most Optimized)
Understand the architectural evolution from naive baseline to optimal complexity.
| Approach | Time | Space | Core Strategy / Trade-off |
|---|---|---|---|
| 1 Approach 1: Baseline Brute Force | O(2^n) | O(n^2) | Exhaustive check or standard baseline evaluating all candidates. |
| 2 Approach 2: Optimal Interview Solution | O(2^n) | O(n) | Optimal algorithmic solution utilizing direct state transitions and optimal data structures. |
Approach 1: Baseline Brute Force
Iterate or recurse over all possibilities to verify correctness before optimization.
Approach 1: Baseline Brute Force Implementation
Approach 1: Baseline Brute Force
class Solution {
public void findCombinationSum2(int index, int candidates[], int target, List<List<Integer>> result, List<Integer> ds){
if(target == 0){
result.add(new ArrayList<>(ds));
return;
}
for(int i = index; i < candidates.length; i++){
if(i > index && candidates[i] == candidates[i-1]){
continue;
}
if(candidates[i] > target){
break;
}
ds.add(candidates[i]);
findCombinationSum2(i+1, candidates, target - candidates[i], result, ds);
ds.remove(ds.size()-1);
}
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> ds = new ArrayList<>();
Arrays.sort(candidates);
findCombinationSum2(0, candidates, target, result, ds);
return result;
}
}class Solution {
public:
void findCombinationSum2(int index, vector<int> &candidates, int target, vector<vector<int>> &result, vector<int> &ds){
if(target == 0){
result.push_back(ds);
}
for(int i = index; i < candidates.size(); i++){
if(i > index && candidates[i] == candidates[i-1]){
continue;
}
if(candidates[i] > target){
break;
}
ds.push_back(candidates[i]);
findCombinationSum2(i+1, candidates, target - candidates[i], result, ds);
ds.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> result;
vector<int> ds;
sort(candidates.begin(), candidates.end());
findCombinationSum2(0, candidates, target, result, ds);
return result;
}
};class Solution:
def combination_sum_ii(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for Combination Sum 2
*/
function combinationSumIi(...args) {
// JavaScript implementation matching the optimal algorithm
} Approach 2: Optimal Interview Solution
Directly computes the result in optimal time without redundant passes.
Approach 2: Optimal Interview Solution Implementation
Approach 2: Optimal Interview Solution
class Solution {
public void findCombinationSum2(int index, int candidates[], int target, List<List<Integer>> result, List<Integer> ds){
if(target == 0){
result.add(new ArrayList<>(ds));
return;
}
for(int i = index; i < candidates.length; i++){
if(i > index && candidates[i] == candidates[i-1]){
continue;
}
if(candidates[i] > target){
break;
}
ds.add(candidates[i]);
findCombinationSum2(i+1, candidates, target - candidates[i], result, ds);
ds.remove(ds.size()-1);
}
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> ds = new ArrayList<>();
Arrays.sort(candidates);
findCombinationSum2(0, candidates, target, result, ds);
return result;
}
}class Solution {
public:
void findCombinationSum2(int index, vector<int> &candidates, int target, vector<vector<int>> &result, vector<int> &ds){
if(target == 0){
result.push_back(ds);
}
for(int i = index; i < candidates.size(); i++){
if(i > index && candidates[i] == candidates[i-1]){
continue;
}
if(candidates[i] > target){
break;
}
ds.push_back(candidates[i]);
findCombinationSum2(i+1, candidates, target - candidates[i], result, ds);
ds.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> result;
vector<int> ds;
sort(candidates.begin(), candidates.end());
findCombinationSum2(0, candidates, target, result, ds);
return result;
}
};class Solution:
def combination_sum_ii(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for Combination Sum 2
*/
function combinationSumIi(...args) {
// JavaScript implementation matching the optimal algorithm
} Edge Cases to Watch
- Empty or single-element input (n <= 1)
- Duplicates and boundary values near extrema
- Already sorted, reverse sorted, or uniform values
- Maximum constraint inputs to ensure no integer overflow or TLE
Master the Backtracking Pattern
Study the core invariant, step-by-step decision framework, and common interview traps for the Backtracking pattern.
How to Recognize This Pattern
- Duplicates in candidates — skip same value at same level
- Difference from Combination Sum: each number used once
Complexity Analysis
Time Complexity
O(2ⁿ)
Space Complexity
O(n)