<p>Given an array of <strong>distinct</strong> integers <code>nums</code> and a target integer <code>target</code>, return <em>the number of possible combinations that add up to</em>&nbsp;<code>target</code>.</p>

<p>The test cases are generated so that the answer can fit in a <strong>32-bit</strong> integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3], target = 4
<strong>Output:</strong> 7
<strong>Explanation:</strong>
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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [9], target = 3
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 200</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
	<li>All the elements of <code>nums</code> are <strong>unique</strong>.</li>
	<li><code>1 &lt;= target &lt;= 1000</code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> 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?</p>

#include <vector>
#include <unordered_map>
using namespace std;

class Solution {
public:
    // Memoization map to store the results of subproblems
    unordered_map<int, int> memo;
    
    int helper(const vector<int>& nums, int target) {
        // Base case: If the target is zero, there is exactly one way to achieve this
        if (target == 0) {
            return 1;
        }
        
        // Base case: If the target is negative, there is no valid combination
        if (target < 0) {
            return 0;
        }
        
        // Check if the result for this target is already computed
        if (memo.find(target) != memo.end()) {
            return memo[target];
        }
        
        // Initialize the number of combinations for this target
        int count = 0;
        
        // Try each number in the array
        for (int num : nums) {
            // Add the number of combinations that sum up to target - num
            count += helper(nums, target - num);
        }
        
        // Store the result in the memoization map and return it
        memo[target] = count;
        return count;
    }
    
    int combinationSum4(vector<int>& nums, int target) {
        // Start the recursion
        return helper(nums, target);
    }
};

9600