Tutorialspoint
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
NumberBacktracking KPMGD. E. Shaw
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

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

Steps to solve by this approach:

 Step 1: Sort the candidates array to ensure we generate combinations in a systematic way.
 Step 2: Use backtracking with a recursive function that keeps track of the current combination and its sum.
 Step 3: For each recursive call, we try adding each candidate (starting from a specific position) to our current combination.
 Step 4: If the current sum equals the target, we've found a valid combination and add it to our result.
 Step 5: If the current sum exceeds the target, we backtrack (stop exploring this path).
 Step 6: To avoid duplicate combinations, we only consider candidates at or after the current position in the array.
 Step 7: For each candidate, we can use it multiple times by keeping the same start index in the recursive call.

Submitted Code :