Paths Subjects Questions Quizzes Pricing Search
Intermediate Open Pro

Combinations of a Sum

Given an array of distinct positive integers candidates and a target integer target, return all unique combinations of candidates where the chosen numbers sum to target. The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one chosen number differs; combinations may be returned in any order, but each individual combination should list numbers in non-decreasing order to avoid [2,3,2]-style duplicates of [2,2,3].

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
  • 2 <= candidates[i] <= 40
  • All elements of candidates are distinct.
  • 1 <= target <= 40

Share this question

← Back to Backtracking practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.