<p>You are given an integer array <code>nums</code> and an integer <code>target</code>.</p>

<p>You want to build an <strong>expression</strong> out of nums by adding one of the symbols <code>&#39;+&#39;</code> and <code>&#39;-&#39;</code> before each integer in nums and then concatenate all the integers.</p>

<ul>
	<li>For example, if <code>nums = [2, 1]</code>, you can add a <code>&#39;+&#39;</code> before <code>2</code> and a <code>&#39;-&#39;</code> before <code>1</code> and concatenate them to build the expression <code>&quot;+2-1&quot;</code>.</li>
</ul>

<p>Return the number of different <strong>expressions</strong> that you can build, which evaluates to <code>target</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,1,1,1,1], target = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> There are 5 ways to assign symbols to make the sum of nums be target 3.
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
</pre>

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

<pre>
<strong>Input:</strong> nums = [1], target = 1
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 20</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
	<li><code>0 &lt;= sum(nums[i]) &lt;= 1000</code></li>
	<li><code>-1000 &lt;= target &lt;= 1000</code></li>
</ul>

class Solution {
public:
    vector<vector<int>> dp;
    Solution(){
        dp = vector<vector<int>>(21,vector<int> (1001,0));
    }

    int findTargetSumWays(vector<int>& nums, int target) {
        int sum = accumulate(nums.begin(), nums.end(),0);

        int n = nums.size();
        int summ = (sum+ target);
        if(summ % 2 == 1 || summ < 0) return 0;
        summ /= 2;
        helper(nums,summ,n);

        // int ans = 0;

        // for(int i = 0 ; i <= n ; i++){
        //     for(int j = 0 ; j <= summ ; j++){
        //         // ans += dp[i][j];
        //         cout << dp[i][j] <<" ";
        //     }
        //     cout << endl;
        // }
        int x = 0;
        for(int i = 0; i < n; i++){
            if(nums[i] == 0) x++;
        }
        return dp[n][summ] * pow(2,x);

        // return ans;
    }

    int helper(vector<int>& nums, int summ, int n){ // n is no of objects left to choose from// we will keep updating summ
        if(n == 0 && summ != 0)
            return 0;
        if(summ == 0) {
            dp[n][summ]++;
            return 1;
        }
        if(dp[n][summ] != 0) return dp[n][summ];
        if(nums[n-1] == 0){
            return dp[n][summ] = helper(nums,summ,n-1);
        }
        if(nums[n-1] > summ) return dp[n][summ] = helper(nums,summ,n-1);
        if(nums[n-1] <= summ) return dp[n][summ] = helper(nums,summ,n-1) + helper(nums,summ-nums[n-1],n-1);
        return 5;
    }
};
28056