Skip to content
BASA DSA

Combination Sum

MediumBacktracking

Return all distinct combinations of candidate values summing to target. A candidate may be used repeatedly. Ordering within a combination and among combinations is unrestricted.

Example 1

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

Explanation

7 is formed as 2+5 or a single 7; no other combination reaches it.

Example 2

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

Explanation

Multiples of 3 cannot equal 8.

Example 3

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

Explanation

Reuse candidate 4 twice to reach 8.

Constraints

  • 1 <= number of distinct candidates <= 30
  • Candidates are positive integers
  • 1 <= target <= 40

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