Search⌘ K
AI Features

Problem: Combination Sum

Explore how to implement a recursive backtracking solution in C++ that finds all unique combinations of numbers adding up to a target. Learn to handle unlimited reuse of candidates, apply pruning for efficiency, and understand the algorithm's time and space complexity.

Statement

Given an array of distinct integers candidates and a target integer target, return all unique combinations of numbers from candidates that sum up to target. The combinations may be returned in any order.

Each number in candidates may be selected an unlimited number of times. Two combinations are considered unique if they differ in the frequency of at least one chosen number.

Constraints:

  • 11 \leq candidates.length 30\leq 30

  • 22 \leq candidates[i] 40\leq 40

  • All elements of candidates are distinct.

  • 11 \leq target 40\leq 40 ...