304 North Cardinal St.
Dorchester Center, MA 02124

# Combination Sum III LeetCode Solution

## Problem – Combination Sum III LeetCode Solution

Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:

• Only numbers `1` through `9` are used.
• Each number is used at most once.

Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.

Example 1:

``````Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.``````

Example 2:

``````Input: k = 3, n = 9
Output: [[1,2,6],[1,3,5],[2,3,4]]
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
``````

Example 3:

``````Input: k = 4, n = 1
Output: []
Explanation: There are no valid combinations.
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
``````

Constraints:

• `2 <= k <= 9`
• `1 <= n <= 60`

## Combination Sum III LeetCode Solution in Java

`````` public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> ans = new ArrayList<>();
combination(ans, new ArrayList<Integer>(), k, 1, n);
return ans;
}

private void combination(List<List<Integer>> ans, List<Integer> comb, int k,  int start, int n) {
if (comb.size() == k && n == 0) {
List<Integer> li = new ArrayList<Integer>(comb);
return;
}
for (int i = start; i <= 9; i++) {
combination(ans, comb, k, i+1, n-i);
comb.remove(comb.size() - 1);
}
}
``````

## Combination Sum III LeetCode Solution in Python

``````class Solution(object):
def combinationSum3(self, k, n):
ret = []
self.dfs(list(range(1, 10)), k, n, [], ret)
return ret

def dfs(self, nums, k, n, path, ret):
if k < 0 or n < 0:
return
if k == 0 and n == 0:
ret.append(path)
for i in range(len(nums)):
self.dfs(nums[i+1:], k-1, n-nums[i], path+[nums[i]], ret)
``````

## Combination Sum III LeetCode Solution in C++

``````class Solution {
public:
void combination(vector<vector<int>>& result, vector<int> sol, int k, int n) {
if (sol.size() == k && n == 0) { result.push_back(sol); return ; }
if (sol.size() < k) {
for (int i = sol.empty() ? 1 : sol.back() + 1; i <= 9; ++i) {
if (n - i < 0) break;
sol.push_back(i);
combination(result, sol, k, n - i);
sol.pop_back();
}
}
}

vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> result;
vector<int> sol;
combination(result, sol, k, n);
return result;
}
};
``````
##### Combination Sum III LeetCode Solution Review:

In our experience, we suggest you solve this Combination Sum III LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Combination Sum III LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Combination Sum III LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions