AceYourInterviews
Checking sign-in…

123. Combination Sum II — Each Candidate Once

IntermediateSingle-use combination search with local duplicate pruning and positive-sum bounds

Choose input occurrences whose sum equals the target. You may not reuse an occurrence, but equal values at different input positions can both be chosen.

Different position choices yielding the same value combination must be deduplicated. This differs from the reusable-candidate version of combination sum.

Example 1

Input

nums=[10,1,2,7,6,1,5], target=8

Output

[[1,1,6],[1,2,5],[1,7],[2,6]]

Explanation: Two available 1 occurrences can both appear, while equal sibling starts are suppressed.

Example 2

Input

nums=[2], target=4

Output

[]

Explanation: This occurrence can be chosen only once, unlike DSA-122.

Example 3

Input

nums=[MAX,MAX,1], target=MAX

Output

[[MAX]]

Explanation: Two equal MAX occurrences represent one singleton answer; their combined sum does not wrap into an int.

Constraints

  • 0 <= nums.length <= 20
  • 1 <= nums[i] <= 231 - 1
  • 0 <= target <= 231 - 1
Hints
Hint 1

Use the same sibling ownership rule as duplicate subsets.

Hint 2

Each chosen occurrence is consumed, so the child begins after it.

Hint 3

Copy at remaining zero; otherwise skip equal siblings, break on overshoot, recurse from i+1, and undo.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

One-use indices coexist with deeper duplicate values

[1,1,2]
3
[[1,2]]