Back to Practice Dashboard
Top 150 InterviewEasy

Combination Sum II

Learn how to solve the 'Combination Sum II' problem. This detailed resource details brute force and optimized approaches.

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?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

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

Open in Editor