Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Combination Sum problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Combination Sum problem solution, we have given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different. It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Leetcode Combination Sum problem solution

Leetcode Combination Sum problem solution in Python.

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        candidates.sort()
        res = []
        self.helper(0, candidates, target, [], 0, res)
        return res
        
    def helper(self, i, candidates, target, cur, temp, res):
        for j in range(i, len(candidates)):
            if temp + candidates[j] == target:
                res.append(cur + [candidates[j]])
                return 
            elif temp + candidates[j] < target:
                self.helper(j, candidates, target, cur + [candidates[j]], temp + candidates[j], res)
            else:
                return

Combination Sum problem solution in Java.

class Solution {
    
    public List<List<Integer>> combinationSum(int[] candidates, int target)
    {
        List<List<Integer>> scombo = new ArrayList<>();
        tryCombo(candidates, target, 0, new ArrayList<>(), scombo);
        return scombo;
    }

    public void tryCombo(int[] candidates, int target, int idxCandidate, List<Integer> currentCombo, List<List<Integer>> combos)
    {
        if (target == 0)
        {
            combos.add(currentCombo);
            return;
        }
        if (target < 0)
        {
            return;
        }

        for (int i = idxCandidate; i < candidates.length; i++)
        {
            int cur = candidates[i];
            if (target - cur < 0)
            {
                continue;
            }

            List<Integer> list = new ArrayList<>(currentCombo);
            list.add(cur);

            tryCombo(candidates, target - cur, i, list, combos);
        }
    }
}

Problem solution in C++.

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> res;
        vector<int> r;
        combin(res,r,candidates,target,0);
        return res;
    }
    void combin(vector<vector<int>> &res, vector<int> r, vector<int>& c, int t, int s){
        if(s == t){
            cout<<s<<endl;
            sort(r.begin(),r.end());
            if(res.size()<=0){
                res.push_back(r);
            }else{
                bool flag =false;
                for(int i=0; i < res.size(); i++){
                    if(r == res[i]){
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    res.push_back(r);
                }
            }
            
        }else if(s < t){
            for(int i = 0; i <c.size(); i++ ){
                int sum = s+ c[i];
                r.push_back(c[i]);
                combin(res, r,c,t,sum);
                r.pop_back();
            }
        }
    }
};

Problem solution in C.

#define SIZE 125
int** combinationSum(int* candidates, int candidatesSize, int target, int** columnSizes, int* returnSize) {
    int** ret=(int**)malloc(SIZE*sizeof(int*));
    int count=0;
    int tamp_returnSize=0;
    int** tamp_columnSizes=(int**)malloc(sizeof(int*));
    columnSizes[0]=(int*)malloc(SIZE*sizeof(int));
    if(candidatesSize<1||target<0){
        *returnSize=0;
        free(ret);
        free(tamp_columnSizes);
        return NULL;
    }
    for(int i=0;i<candidatesSize;i++){
        if(target-candidates[i]==0){
            ret[count]=(int*)malloc(sizeof(int));
            columnSizes[0][count]=1;
            ret[count][0]=candidates[i];
            count++;
            continue;
        }
        int** tamp=combinationSum(&candidates[i], candidatesSize-i, target-candidates[i], tamp_columnSizes, &tamp_returnSize);
        for(int k=0;k<tamp_returnSize;k++){
            ret[count]=(int*)malloc((tamp_columnSizes[0][k]+1)*sizeof(int));
            columnSizes[0][count]=tamp_columnSizes[0][k]+1;
            ret[count][0]=candidates[i];
            for(int j=0;j<tamp_columnSizes[0][k];j++){
                ret[count][j+1]=tamp[k][j];
            }
            count++;
        }
        free(tamp);
    }
    free(tamp_columnSizes);
    *returnSize=count;
    return ret;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes