KatInterview

377. Combination Sum IV

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the number of ways to form a target sum using numbers from an array nums. We can use each number multiple times. A critical aspect of this problem is that the order of the numbers in a sum matters. For example, (1, 2) and (2, 1) are considered two different ways if the target is 3 and nums contains 1 and 2. This means we are counting permutations or sequences, not just combinations.

Solution

Combination Sum IV in Python

class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        # dp[i] will store the number of combinations that sum up to i.
        dp = [0] * (target + 1)
        
        # Base case: There is one way to make sum 0 (by choosing no numbers).
        dp[0] = 1
        
        # Iterate from 1 up to the target to fill the dp table.
        for i in range(1, target + 1):
            # For each target sum i, iterate through the numbers in nums.
            for num in nums:
                # If the current sum i is greater than or equal to the number,
                # we can form combinations for i by using combinations for (i - num)
                # and then adding the current number `num`.
                if i - num >= 0:
                    dp[i] += dp[i - num]
                            
        # The result is the number of combinations for the target sum.
        # Python integers handle arbitrary size, so no overflow issues.
        return dp[target]

Combination Sum IV in Java

class Solution {
    public int combinationSum4(int[] nums, int target) {
        // dp[i] will store the number of combinations that sum up to i.
        int[] dp = new int[target + 1];

        // Base case: There is one way to make sum 0 (by choosing no numbers).
        dp[0] = 1;

        // Iterate from 1 up to the target to fill the dp table.
        // This outer loop represents the target sum we are trying to achieve.
        for (int i = 1; i <= target; i++) {
            // This inner loop iterates through the available numbers.
            // For each number, we check if it can contribute to forming the sum i.
            for (int num : nums) {
                // If the current sum i is greater than or equal to the number,
                // we can form combinations for i by using combinations for (i - num)
                // and then adding the current number `num`.
                if (i - num >= 0) {
                    // Although the problem guarantees the final answer fits in an int,
                    // intermediate dp values could potentially overflow.
                    // However, based on the problem statement, we assume this is handled
                    // by the test case generation.
                    dp[i] += dp[i - num];
                }
            }
        }

        // The result is the number of combinations for the target sum.
        return dp[target];
    }
}

Combination Sum IV in C++

class Solution {
public:
    int combinationSum4(vector<int>& nums, int target) {
        // dp[i] will store the number of combinations that sum up to i.
        // We use unsigned int to prevent potential overflow for intermediate values,
        // as the number of combinations can grow very quickly.
        // The problem guarantees the final answer fits in a 32-bit signed int.
        vector<unsigned int> dp(target + 1, 0);

        // Base case: There is one way to make sum 0 (by choosing no numbers).
        dp[0] = 1;

        // Iterate from 1 up to the target to fill the dp table.
        for (int i = 1; i <= target; ++i) {
            // For each target sum i, iterate through the numbers in nums.
            for (int num : nums) {
                // If the current sum i is greater than or equal to the number,
                // we can form combinations for i by using combinations for (i - num)
                // and then adding the current number `num`.
                if (i - num >= 0) {
                    dp[i] += dp[i - num];
                }
            }
        }
        
        // The result is the number of combinations for the target sum.
        return dp[target];
    }
};

Combination Sum IV in TypeScript

function combinationSum4(nums: number[], target: number): number {
    // dp[i] will store the number of combinations that sum up to i.
    // In JavaScript/TypeScript, numbers are 64-bit floats, which have a large enough
    // safe integer range to prevent overflow for intermediate steps with the given constraints.
    const dp: number[] = new Array(target + 1).fill(0);

    // Base case: There is one way to make sum 0 (by choosing no numbers).
    dp[0] = 1;

    // Iterate from 1 up to the target to fill the dp table.
    for (let i = 1; i <= target; i++) {
        // For each target sum i, iterate through the numbers in nums.
        for (const num of nums) {
            // If the current sum i is greater than or equal to the number,
            // we can form combinations for i by using combinations for (i - num)
            // and then adding the current number `num`.
            if (i - num >= 0) {
                dp[i] += dp[i - num];
            }
        }
    }

    // The result is the number of combinations for the target sum.
    return dp[target];
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.