Combination Sum

Asked byAmazonFacebookMicrosoftAirbnbAppleAdobe

Problem

Given a list of distinct positive integers and a target, return all unique combinations where the chosen numbers sum to the target. The same number may be reused an unlimited number of times.

Examples

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]]

Constraints

  • 1 <= candidates.length <= 30

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Backtracking pattern

Explore a decision one choice at a time, undo it if it doesn't lead anywhere, and try the next option. It's a systematic way to generate every valid combination, permutation, or arrangement.

Look for this pattern when

  • The problem asks for "all possible" combinations, subsets, permutations, or valid arrangements.
  • A greedy or direct-formula approach doesn't work because you genuinely need to explore multiple branches.

Read the full Backtracking guide →

Video walkthroughs

Original problem on LeetCode ↗

More Backtracking problems