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 II problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Combination Sum II problem solution, we have given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. Each number in candidates may only be used once in the combination.

Leetcode Combination Sum II problem solution

Leetcode Combination Sum II problem solution in Python.

def combinationSum2(self, nums, target):
        def bt(tlist, start, target):
            s = sum(tlist)
            if s == target:
                res.append(tlist[::])
                return
            if s > target:
                return

            for i in range(start, len(nums)):
                if i > start and nums[i] == nums[i - 1]:
                    continue
                tlist.append(nums[i])
                bt(tlist, i + 1, target)
                tlist.pop()
        nums.sort()
        res = []
        bt([], 0, target)
        return res

Combination Sum II problem solution in Java.

class Solution {
    HashSet<List<Integer>> set = new HashSet<>();

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<Integer> tempList = new ArrayList<>();
        backtrack(tempList,candidates,target,0);
        List<List<Integer>> res = new ArrayList<>();
        for(List<Integer> ls:set){
            res.add(ls);
        }
        return res;
    }
    
    public void backtrack(List<Integer> tempList,int[] candidates,int needed_target,int start){
        if(needed_target==0){
            set.add(new ArrayList<>(tempList));
            return;
        }
        for(int i=start;i<candidates.length;i++){
            if(candidates[i]>needed_target) continue;
            tempList.add(candidates[i]);
            backtrack(tempList,candidates,needed_target-candidates[i],i+1);
            tempList.remove(tempList.size()-1);
        }
    }
}

Problem solution in C++.

#define pb push_back
class Solution {
public:
    vector<vector<int>> res;
    void getlist(vector<int>& ar,int index,int target , vector<int>& s){
        int sum = accumulate(s.begin(),s.end(),0);
        if(sum == target){
            res.pb(s);
            return;
        }
        if(sum > target || ar.size() <= index){
            return;
        }
        getlist(ar,index+1,target,s);
        s.pb(ar[index]);
        getlist(ar,index+1,target,s);
        s.pop_back();
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<int> s;
        getlist(candidates,0,target,s);
        vector<vector<int>> ans;
        set<vector<int>> st;
        for(auto x : res){
            vector<int> temp = x;
            sort(temp.begin(),temp.end());
            st.insert(temp);
        }
        for(auto x : st){
            ans.pb(x);
        }
        return ans;
    }
};

Problem solution in C.

void dfs(int* nums, int* colSizes, int numsSize, int target, 
         int** sols, int* sols_size, int* sol, int sol_size, int pointer) {
  if(target < 0) return;
  else if(target == 0) {
    sols[*sols_size] = calloc(sol_size, sizeof(int));
    for(int i = 0; i < sol_size; i++) sols[*sols_size][i] = sol[i];
    colSizes[*sols_size] = sol_size;
    (*sols_size)++;
  }
  else{
    for(int i = pointer; i < numsSize; i++) {
      if(nums[i] > target) return;
      if(i != pointer && nums[i] == nums[i - 1]) continue;
      sol[sol_size] = nums[i];
      dfs(nums, colSizes, numsSize, target - nums[i], 
          sols, sols_size, sol, sol_size + 1, i + 1);
    }
  }
}

int comp(const void* v1, const void* v2) {
  return (*(int*) v1) - (*(int*) v2);
}

int** combinationSum2(int* nums, int numsSize, int target, int** colSizes, int* returnSize) {
  int** sols = calloc(500, sizeof(int*));
  *colSizes = calloc(500, sizeof(int));
  int* sol = calloc(500, sizeof(int));
  qsort((void*) nums, numsSize, sizeof(int), comp);
  dfs(nums, *colSizes, numsSize, target, sols, returnSize, sol, 0, 0);
  return sols;
}

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