ExamAdda Logo

Combination Sum

Easy

Given an array of distinct integers candidates and an integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target.

You may use the same number from candidates an unlimited number of times.

The combinations can be returned in any order.

Two combinations are considered different if the frequency of at least one chosen number is different.

Example 1

Input

n = 3
candidates = [2, 3, 6]
target = 7

Output

2 2 3

Explanation

The only combination that sums to 7 is:

2 + 2 + 3 = 7

 

The number 2 can be used more than once.

Example 2

Input

n = 4
candidates = [2, 3, 6, 7]
target = 7

Output

2 2 3
7

Explanation

There are two valid combinations:

2 + 2 + 3 = 7

7 = 7

Constraints

1 <= candidates.length <= 30
2 <= candidates[i] <= 40
All elements in candidates are distinct.
1 <= target <= 40
The same candidate may be chosen unlimited times.
All numbers in a combination must come from candidates.

Hints:

Hint 1

Use backtracking to build a combination one number at a time.

Hint 2

Because a candidate can be used multiple times, after choosing a candidate, continue the recursive call from the same index.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 20, 2026

Last updated

Aug 21, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output