 All Problems
Combination Sum
medium
backtracking
arrays
amazon
google
microsoft

Given an array of distinct integers candidates and a target integer target, return all unique combinations of candidates where the chosen numbers sum to target. You may use the same number an unlimited number of times.

The combinations may be returned in any order, but each combination must be sorted in ascending order, and the list of combinations should be in lexicographic order.

Example 1:

Input:
2 3 6 7
7
Output:
2 2 3
7

Example 2:

Input:
2 3 5
8
Output:
2 2 2 2
2 3 3
3 5

Constraints:

  • 1 ≤ candidates.length ≤ 30
  • 2 ≤ candidates[i] ≤ 40
  • All elements of candidates are distinct.
  • 1 ≤ target ≤ 40

Input format: First line: space-separated candidates. Second line: target.

Output format: Each valid combination on its own line, elements sorted ascending, combinations in lexicographic order.

Run to check your code against the sample cases, or submit to run every case