{x}
blog image

Combination Sum IV

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 = 4
Output: 7
Explanation:
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 = 3
Output: 0

 

Constraints:

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 1000
  • All the elements of nums are unique.
  • 1 <= target <= 1000

 

Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?

Solution Explanation: Combination Sum IV

This problem asks for the number of distinct combinations of numbers from a given array nums that sum up to a target value target. The key is that different sequences are considered different combinations. This suggests a dynamic programming approach.

Approach: Dynamic Programming

We use dynamic programming to solve this problem efficiently. We create a DP array f of size target + 1, where f[i] represents the number of combinations that sum up to i.

  1. Initialization: f[0] = 1. This is because there's one way to achieve a sum of 0 (using no numbers). All other f[i] are initialized to 0.

  2. Iteration: We iterate through the DP array from index 1 up to target. For each index i, we iterate through the numbers in nums. If a number x is less than or equal to i, it means we can potentially use x in a combination that sums to i. The number of ways to reach i using x is equal to the number of ways to reach i - x (because we've already used x). Therefore, we add f[i - x] to f[i].

  3. Result: After iterating through all indices and numbers, f[target] will contain the total number of combinations that sum up to target.

Time Complexity: O(n * target), where n is the length of nums and target is the target sum. We have nested loops iterating through nums and the f array.

Space Complexity: O(target). The space used is dominated by the DP array f.

Code Examples:

The code examples in multiple languages provided in the original response demonstrate this dynamic programming approach. The core logic remains consistent across all languages:

Python3:

class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        f = [0] * (target + 1)
        f[0] = 1  # Base case: one way to sum to 0
        for i in range(1, target + 1):
            for x in nums:
                if i >= x:
                    f[i] += f[i - x]
        return f[target]
 

Java:

class Solution {
    public int combinationSum4(int[] nums, int target) {
        int[] dp = new int[target + 1];
        dp[0] = 1;
        for (int i = 1; i <= target; i++) {
            for (int num : nums) {
                if (i >= num) {
                    dp[i] += dp[i - num];
                }
            }
        }
        return dp[target];
    }
}

The other languages (C++, Go, TypeScript, JavaScript, C#) follow a similar structure, only differing in syntax. All implementations achieve the same time and space complexity.