# Target Sum

**Problem Statement**

You are given a list of non-negative integers, `a1, a2, ..., an`

, and a target, `S`

. Now you have 2 symbols `+`

and `-`

. For each integer, you should choose one from `+`

and `-`

as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target `S`

.

**Example 1**

Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5

**Explanation**

-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

There are 5 ways to assign symbols to make the sum of `nums`

be target `3`

.

**Note**

The length of the given array is positive and will not exceed 20.

The sum of elements in the given array will not exceed 1000.

Your output answer is guaranteed to be fitted in a 32-bit integer.

**Solution**

This is similar to the Number of Dice Rolls With Target Sum problem. We use dynamic programming (DP).

The idea behind this approach is as follows. Suppose we can find out the number of times a particular sum, say , is possible up to a particular index, say , in the given `nums`

array, which is given by say . Now, we can find out the number of times the sum can occur easily as . Similarly, the number of times the sum occurs is also given by .

Thus, if we know all the sums ‘s which are possible up to the index by using various assignments, along with the corresponding count of assignments, , leading to the same sum, we can determine all the sums possible up to the index along with the corresponding count of assignments leading to the new sums.

Based on this idea, we make use of a to determine the number of assignments which can lead to the given sum.

refers to the number of assignments which can lead to a sum of up to the index. To determine the number of assignments which can lead to a sum of up to the index, we can use which is the same as . Similarly, which is the same as . We iterate over the array in a row-wise fashion i.e. Firstly we obtain all the sums which are possible up to a particular index along with the corresponding count of assignments and then proceed for the next element(index) in the

`nums`

array.

But, since the can range from to , we need to add an offset of to the sum indices (column number) to map all the sums obtained to positive range only.

At the end, the value of gives us the required number of assignments. Here, refers to the number of elements in the array.

class Solution { public int findTargetSumWays(int[] nums, int target) { final int MAX = 1000; int n = nums.length; // dp[i][j] = ways to make sum j if using nums until index i (inclusive) // we need to represent a sum range between -MAX and MAX, inclusive int[][] dp = new int[n][2*MAX+1]; // -MAX ... 0 ... MAX dp[0][MAX + nums[0]] = 1; // for '+' nums[0] dp[0][MAX - nums[0]] += 1; // for '-' nums[0] for (int i=1; i= -1*MAX) { dp[i][MAX + t] += dp[i-1][MAX + (t - nums[i])]; // for '+' nums[i] } if (t + nums[i] <= MAX) { dp[i][MAX + t] += dp[i-1][MAX + (t + nums[i])]; // for '-' nums[i] } } } if (target> MAX) { return 0; } else { return dp[n-1][MAX + target]; } } }