v9.1.1
Menu

Combination Sum

mediumFREEBacktrackingBlind 75

Given an array of distinct integers candidates and a target integer target, return all unique combinations where the chosen numbers sum to target. The same number may be chosen unlimited times. Output each combination sorted, one per line, combinations sorted lexicographically.

Example

Input
2,3,6,7
7
Output
2 2 3
7
Reasoning
  • The problem starts by checking all possible combinations of the given numbers 2,3,6,72, 3, 6, 7 that sum to the target 77.
  • It finds two valid combinations: 2+2+3=72 + 2 + 3 = 7 and 7=77 = 7, where the same number can be chosen unlimited times.
  • These combinations are then sorted and output one per line, with the combinations themselves sorted lexicographically: 2232 2 3 and 77.
  • The final output is a list of these combinations, each on a new line, resulting in: 2 2 3 7

Constraints

  • 1 <= len(candidates) <= 30
  • 2 <= candidates[i] <= 40
  • 1 <= target <= 40

Learn this in

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.