
Problem
Solution
Submissions
Combination Sum
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a C program to find all unique combinations of candidates where the candidate numbers sum to a target. The same number from the candidates array may be chosen unlimited number of times. All numbers in the candidates array are distinct and positive integers. The solution set must not contain duplicate combinations.
Example 1
- Input: candidates = [2,3,6,7], target = 7
- Output: [[2,2,3],[7]]
- Explanation: We need to find all combinations that sum to 7. One combination is [2,2,3] because 2+2+3=7. Another combination is [7] because 7=7. These are the only two combinations possible.
Example 2
- Input: candidates = [2,3,5], target = 8
- Output: [[2,2,2,2],[2,3,3],[3,5]]
- Explanation: We need to find all combinations that sum to 8. First combination is [2,2,2,2] because 2+2+2+2=8. Second combination is [2,3,3] because 2+3+3=8. Third combination is [3,5] because 3+5=8.
Constraints
- 1 <= candidates.length <= 30
- 1 <= candidates[i] <= 200
- All elements of candidates are distinct
- 1 <= target <= 500
- The solution set must not contain duplicate combinations
- Time Complexity: O(N^(T/M)), where N is the number of candidates, T is the target value, and M is the minimum value among candidates
- Space Complexity: O(T/M) for the recursion depth
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use backtracking to find all unique combinations
- Sort the candidates array first to handle combinations efficiently
- For each candidate, you have two choices: include it in the current combination or skip it
- If including a candidate, you can reuse it in subsequent steps
- When the sum equals the target, you've found a valid combination
- If the sum exceeds the target, backtrack and try another path
- Use a dynamic array to store each valid combination