Skip to content
AI360Xpert
Beta
Difficulty: MediumBacktracking

Combination Sum

Problem in Plain English

Given distinct candidates and a target, return every unique combination that sums to the target while reusing any candidate as many times as needed. Each combination appears once regardless of internal order.

Problem Statement

Given an array of distinct integers candidates and an integer target, return every unique combination of candidates whose values sum to target.

Any candidate may be reused an unlimited number of times, and two combinations count as the same when they hold the same multiset of numbers.

Return the full list in any order.

Constraints

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • All elements of candidates are distinct.
  • 1 <= target <= 40
  • The input has fewer than 150 unique combinations summing to the target.

Examples

Example 1

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

Two copies of 2 and one 3 total 7; the candidate 7 also reaches the target alone. No other multiset works. [3,2,2] is the same multiset as [2,2,3], so it must not appear separately.

Example 2

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

Three multisets reach 8: four 2 values, [2, 3, 3], and [3, 5].

Example 3

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

No multiple of 2 equals 1, so the answer is empty.

Intuition

A combination records how many times each value is used, so [2,2,3] and [3,2,2] are the same answer. Backtracking grows a temporary path, explores its continuations, then removes the last choice before trying a sibling. In Example 1, two 2s leave 3 to reach 7. Allowing the same index again permits reuse; allowing only that index or later ones prevents different orders of the same multiset.

Approaches

Ordered Search + Dedup

Solution Details

Reveal Ordered Search + Dedup: intuition, complexity, and code

Hints

Hint 1
Unlimited reuse means the same candidate can follow itself, so your recursion must be allowed to stay in place rather than always move forward.
Hint 2
Trying candidates in every order finds the same multiset many times, so force a nondecreasing order with a start index.
Hint 3
Sort first: then any candidate that overshoots the remainder lets you break the loop instead of scanning the rest.

Edge Cases

  • No solution: [2] with target 1 returns [], because even the smallest candidate is too large.
  • One candidate can be reused: [2] with target 8 returns [[2,2,2,2]].
  • A candidate equal to target is a singleton answer; [7] with target 7 returns [[7]].
  • Unsorted input [7,3,2,6] with target 7 has the same two answers as Example 1. Numeric sorting is required only for the pruning approach.
  • At the maximum target 40, candidate 2 builds a path of length 20. Positive values bound recursion depth; zero or negative values would invalidate this argument.

Common Mistakes and Interview Tips

  • Always advancing the index after taking a candidate, which bans reuse and misses answers like [2, 2, 3].
  • Skipping the sort and the start index, which emits permutations of the same combination as if they were different answers.
  • Breaking on overshoot without sorting first, which wrongly discards larger-looking candidates that appear before smaller ones.
  • Saving the mutable path itself lets subsequent pops erase earlier answers; copy every accepted path.
  • Confusing runtime with result count: the ordered approach can explore many permutations even when the number of unique answers is small. Explain this trade-off in an interview.

Key Takeaway

Unlimited-reuse searches stay duplicate-free by imposing an order: sort the choices and let each position pick only from its own index onward.