Table of Contents

# Combination Sum IV – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

Given an array of **distinct** integers `nums`

and a target integer `target`

, return *the number of possible combinations that add up to* `target`

.

The test cases are generated so that the answer can fit in a **32-bit** integer.

**Example 1:**

Input:nums = [1,2,3], target = 4Output:7Explanation:The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1) Note that different sequences are counted as different combinations.

**Example 2:**

Input:nums = [9], target = 3Output:0

**Constraints:**

`1 <= nums.length <= 200`

`1 <= nums[i] <= 1000`

- All the elements of
`nums`

are**unique**. `1 <= target <= 1000`

# C++ Combination Sum IV LeetCode Solution

```
``````
class Solution {
public:
/**
* @param A: an integer array.
* @param k: a positive integer (k <= length(A))
* @param target: a integer
* @return an integer
*/
int kSum(vector<int> A, int k, int target) {
// wirte your code here
const int n = A.size();
/** dp[i][j][target] : # of ways to start from vector[0..i-1], choose j elements to sum to target **/
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(k + 1, vector<int>(target + 1, 0)));
for(int i = 1; i <= n; i++) {
if(A[i-1] <= target) {
for(int j = i; j <= n; j++) {
dp[j][1][A[i-1]] =1;
}
}
}
/** for position i, we can choose it or not **/
for(int i = 1; i <= n; i++) {
for(int j = min(i, k); j > 1; j--) {
for(int p = 1; p <= target; p++) {
dp[i][j][p] = dp[i - 1][j][p];
if(p - A[i - 1] >= 0) {
dp[i][j][p] += dp[i - 1][j - 1][p - A[i - 1]];
}
}
}
}
return dp[n][k][target];
}
};
```

# Java Combination Sum IV LeetCode Solution

```
``````
private int[] dp;
public int combinationSum4(int[] nums, int target) {
dp = new int[target + 1];
Arrays.fill(dp, -1);
dp[0] = 1;
return helper(nums, target);
}
private int helper(int[] nums, int target) {
if (dp[target] != -1) {
return dp[target];
}
int res = 0;
for (int i = 0; i < nums.length; i++) {
if (target >= nums[i]) {
res += helper(nums, target - nums[i]);
}
}
dp[target] = res;
return res;
}
```

# Python 3 Combination Sum IV LeetCode Solution

```
``````
class Solution(object):
def combinationSum4WithLength(self, nums, target, length, memo=collections.defaultdict(int)):
if length <= 0: return 0
if length == 1: return 1 * (target in nums)
if (target, length) not in memo:
for num in nums:
memo[target, length] += self.combinationSum4(nums, target - num, length - 1)
return memo[target, length]
```

Array-1180

String-562

Hash Table-412

Dynamic Programming-390

Math-368

Sorting-264

Greedy-257

Depth-First Search-256

Database-215

Breadth-First Search-200

Tree-195

Binary Search-191

Matrix-176

Binary Tree-160

Two Pointers-151

Bit Manipulation-140

Stack-133

Heap (Priority Queue)-117

Design-116

Graph-108

Simulation-103

Prefix Sum-96

Backtracking-92

Counting-86

Sliding Window-73

Linked List-69

Union Find-66

Ordered Set-48

Monotonic Stack-47

Recursion-43

Trie-41

Binary Search Tree-40

Divide and Conquer-40

Enumeration-39

Bitmask-37

Queue-33

Memoization-32

Topological Sort-31

Geometry-30

Segment Tree-27

Game Theory-24

Hash Function-24

Binary Indexed Tree-21

Interactive-18

Data Stream-17

String Matching-17

Rolling Hash-17

Shortest Path-16

Number Theory-16

Combinatorics-15

Randomized-12

Monotonic Queue-9

Iterator-9

Merge Sort-9

Concurrency-9

Doubly-Linked List-8

Brainteaser-8

Probability and Statistics-7

Quickselect-7

Bucket Sort-6

Suffix Array-6

Minimum Spanning Tree-5

Counting Sort-5

Shell-4

Line Sweep-4

Reservoir Sampling-4

Eulerian Circuit-3

Radix Sort-3

Strongly Connected Componen-t2

Rejection Sampling-2

Biconnected Component-1