# Combination Sum LeetCode Solution

Here, We see Combination Sum problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

## Problem Statement ->

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.

```Example 1:
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.```
```Example 2:
Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]

Example 3:
Input: candidates = , target = 1
Output: []```

## Combination Sum Leetcode Solution C++ ->

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```class Solution {
public:
vector<vector<int>> result;
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<int> curr;
int sum = 0;
int n = candidates.size();
comSum(curr, 0, sum, candidates, target, n);
return result;
}
void comSum(vector<int> &curr, int curInd, int sum, vector<int> &candidates, int target, int n){
if(sum == target){
result.push_back(curr);
return;
}
else if(sum > target){
return;
}
for(int i = curInd; i < n; i++){
curr.push_back(candidates[i]);
sum += candidates[i];
comSum(curr, i, sum, candidates, target, n);
sum -= candidates[i];
curr.pop_back();
}
}
};
```Code language: C++ (cpp)```

## Combination Sum Leetcode Solution Java ->

``````class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(candidates);
backtrack(list, new ArrayList<>(), candidates, target, 0);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] candidates, int remain, int start){
if(remain < 0) return;
else if(remain == 0) list.add(new ArrayList<>(tempList));
else{
for(int i = start; i < candidates.length; i++){
backtrack(list, tempList, candidates, remain - candidates[i], i); // not i + 1 because we can reuse same elements
tempList.remove(tempList.size() - 1);
}
}
}
}
```Code language: Java (java)```

## Combination Sum Leetcode Solution JavaScript ->

``````var combinationSum = function(candidates, target) {
var buffer = [];
var result = [];
search(0, target);
return result;

function search(startIdx, target) {
if (target === 0) return result.push(buffer.slice());
if (target < 0) return;
if (startIdx === candidates.length) return;
buffer.push(candidates[startIdx]);
search(startIdx, target - candidates[startIdx]);
buffer.pop();
search(startIdx + 1, target);
}
};
```Code language: JavaScript (javascript)```

## Combination Sum Leetcode Solution Python ->

``````class Solution(object):
def combinationSum(self, candidates, target):
result = []
candidates = sorted(candidates)
def dfs(remain, stack):
if remain == 0:
result.append(stack)
return
for item in candidates:
if item > remain: break
if stack and item < stack[-1]: continue
else:
dfs(remain - item, stack + [item])

dfs(target, [])
return result
```Code language: Python (python)```

#### Sudoku Solver LeetCode Solution

Write a program to solve a Sudoku puzzle by filling the empty cells.

#### Contains Duplicate II LeetCode Solution

Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such…

#### Find the Index of the First Occurrence in a String LeetCode Solution

Given two strings needle and haystack, return the index of the first occurrence of needle…

#### Reverse Linked List LeetCode Solution

Given the head of a singly linked list, reverse the list, and return the reversed list.

#### Pow(x n) LeetCode Solution

Implement pow(x, n), which calculates x raised to the power n.

#### Search in Rotated Sorted Array LeetCode Solution

Given the array nums after the possible rotation and an integer target, return the index…