Skip to content
Onsite

Combination Sum

MediumP1Coding

Given an array of distinct positive integers candidates and a positive integer target, return all unique combinations of candidates that sum to target. A candidate may be used any number of times.

Two combinations are the same if they use each number the same number of times, regardless of order. Return the combinations in any order, and the numbers inside each combination in any order. Return [] if there is none.

Example 1

Input
candidates = [2,3,6,7], target = 7
Output
[[2,2,3],[7]]

Example 2

Input
candidates = [2,3,5], target = 8
Output
[[2,2,2,2],[2,3,3],[3,5]]

Example 3

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

Constraints

  • 1 <= candidates.length <= 10
  • 2 <= candidates[i] <= 40, all distinct
  • 1 <= target <= 40

Hints

Run checks the 3 visible tests, Submit all 10.