Skip to content
BASA DSA

Combination Sum II

MediumBacktracking

Return all distinct combinations of array entries summing to target. Each index may be used at most once; duplicate input values must not cause duplicate combinations.

Example 1

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

Explanation

Use 1+3 or the two separate 1 occurrences plus 2; no index is reused.

Example 2

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

Explanation

Choosing any two of three equal 2s yields the same combination, returned once.

Example 3

Input
candidates = [5]
target = 3
Output
[]

Explanation

The sole value 5 exceeds target 3.

Constraints

  • 1 <= len(candidates) <= 100
  • Values are positive integers
  • 1 <= target <= 30

Function contractcombinationSum2Arguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…