Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Target Sum

Sharing is caring!

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 sum_i, is possible up to a particular index, say i, in the given nums array, which is given by say count_i. Now, we can find out the number of times the sum sum_i + nums\left[i\right] can occur easily as count_i. Similarly, the number of times the sum sum_i - nums\left[i\right] occurs is also given by count_i.

Thus, if we know all the sums sum_j‘s which are possible up to the j^{th} index by using various assignments, along with the corresponding count of assignments, count_j, leading to the same sum, we can determine all the sums possible up to the (j + 1)^{th} index along with the corresponding count of assignments leading to the new sums.

Based on this idea, we make use of a dp to determine the number of assignments which can lead to the given sum.
dp\left[i\right]\left[j\right] refers to the number of assignments which can lead to a sum of j up to the i^{th} index. To determine the number of assignments which can lead to a sum of sum + nums\left[i\right] up to the (i + 1)^{th} index, we can use dp\left[i\right]\left[sum+nums\left[i\right]\right]=dp\left[i\right]\left[sum+nums\left[i\right]\right]+dp\left[i-1\right]\left[sum\right] which is the same as dp\left[i\right]\left[sum\right]=dp\left[i\right]\left[sum\right]+dp\left[i-1\right]\left[sum-nums\left[i\right]\right]. Similarly, dp\left[i\right]\left[sum - nums\left[i\right]\right] = dp\left[i\right]\left[sum - nums\left[i\right]\right] + dp\left[i - 1\right]\left[sum\right] which is the same as dp\left[i\right]\left[sum\right]=dp\left[i\right]\left[sum\right]+dp\left[i-1\right]\left[sum+nums\left[i\right]\right]. We iterate over the dp 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 sum can range from -1000 to +1000, we need to add an offset of 1000 to the sum indices (column number) to map all the sums obtained to positive range only.

At the end, the value of dp\left[n-1\right]\left[S+1000\right] gives us the required number of assignments. Here, n refers to the number of elements in the nums 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];
        }
    }
}