Skip to content
AI360Xpert
Beta
Difficulty: MediumBacktracking

Combination Sum II

Problem in Plain English

Given candidates that may repeat and a target, return every unique combination summing to the target while spending each element at most once. Duplicate combinations appear only once.

Problem Statement

Given a collection of candidate numbers candidates that may hold duplicates, plus an integer target, return every unique combination whose values sum to target.

Each element may be used at most once, so two equal values at different positions are still two separate one-time picks.

The answer must contain no duplicate combinations; return it in any order.

Constraints

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

Examples

Example 1

Input
candidates = [10,1,2,7,6,1,5], target = 8
Output
[[1,1,6],[1,2,5],[1,7],[2,6]]

The four listed combinations sum to 8. Both 1s are needed for [1,1,6]; either 1 can form [1,7], but those choices describe the same value combination and appear once. The value 10 is too large.

Example 2

Input
candidates = [2,5,2,1,2], target = 5
Output
[[1,2,2],[5]]

Using 1 and two separate 2 positions makes [1,2,2]; the lone 5 makes [5]. The three possible ways to choose two of the three 2s collapse to one value combination.

Example 3

Input
candidates = [1], target = 2
Output
[]

The only available value is 1. Reusing it would make 2, but each position can be used once, so no valid combination exists.

Intuition

Equal values are interchangeable as answers, but their positions are separate resources. Example 1 contains two 1s, so [1,1,6] is legal, while choosing either 1 for [1,7] must produce only one answer. Backtracking builds a partial combination and undoes each choice. Sorting lets us skip equal alternatives at the same decision level while still using a second equal value deeper in the path.

Approaches

Subset Filter + Dedup

Solution Details

Reveal Subset Filter + Dedup: intuition, complexity, and code

Hints

Hint 1
Treat every position as a one-time resource; choosing one of two equal values must not prevent choosing the other later.
Hint 2
An include/skip search is complete, but duplicate values can produce the same answer through different positions. How could sorted successful paths serve as deduplication keys?
Hint 3
Sort once, advance to i + 1 after a pick, and skip a repeated value only when i > start at the current level. Positivity also lets a too-large sorted value terminate the loop.

Edge Cases

  • All equal values: [1,1,1] with target 2 returns [[1,1]], using two distinct positions rather than deleting all duplicates.
  • A single value can be an exact answer: [5] with target 5 returns [[5]]. A single 1 with target 2 returns [].
  • When every value exceeds the target, the sorted approach breaks immediately; [50,50] with target 30 returns [].
  • Multiple equal candidates need enough occurrences: [2,2] with target 6 has no answer, while [2,2,2] returns [[2,2,2]].
  • The target bound limits each successful path to at most 30 values because every candidate is positive; the include/skip call stack can still reach all 100 positions.

Common Mistakes and Interview Tips

  • Skipping every repeated value, including at a deeper level, deletes [1,1,6]. The condition is i > start, not i > 0.
  • Recursing from i instead of i + 1 permits unlimited reuse and answers Combination Sum rather than Combination Sum II.
  • Sorting JavaScript without a numeric comparator puts 10 before 2 and breaks the pruning rule.
  • Saving the mutable path directly makes earlier results change during backtracking; copy it at every successful leaf.
  • Claiming constant or linear total space while ignoring stored output and successful deduplication keys hides potentially large allocations.
  • Interview tip: state the difference between duplicate sibling choices and repeated values along one path, and explain that sorting mutates the input in these snippets.

Key Takeaway

Single-use searches with duplicates stay unique through one rule: sort first, then never start the same value twice at the same level.