Top 150 InterviewEasy

Combination Sum II

Detailed guide and Python implementation for the 'Combination Sum II' problem.

Problem Statement

Easy

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

Each number in candidates may only be used once in the combination.

Note: The solution set must not contain duplicate combinations.

Implement a function combinationSum2(candidates: list, target: int) -> list.

Constraints
  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30

Examples

Example 1
Input
[10,1,2,7,6,1,5], 8
Output
[[1,1,6],[1,2,5],[1,7],[2,6]]
Explanation

All unique combinations that sum to 8, using each element at most once.

Example 2
Input
[2,5,2,1,2], 5
Output
[[1,2,2],[5]]
Explanation

1+2+2 = 5 and 5 = 5. These are the only unique combinations.

Need a Hint?
Consider using Backtracking-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.