WorkspaceProblem lab
ALGORITHM VISUALIZER

Combination Sum

Opening the interactive workspace…

Understanding Combination Sum

Return every unique combination of candidates that sums to target. A candidate may be used repeatedly; order within a combination does not matter.

How it works

Restrict later choices to the current index or beyond to allow reuse without generating reordered duplicates.

Sort the candidates and start with an empty path. Choose a value no larger than the remaining sum. Recurse from the same index, then undo the choice.

Try an example

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

Output: [[2, 2, 3], [7]]

Restrict later choices to the current index or beyond to allow reuse without generating reordered duplicates.

Constraints

  • 0–6 distinct positive integer candidates, each at most 20.
  • Target is an integer from 0 through 12.
  • For target 0, return the single empty combination.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity