Target Sum

Given an array nums and a target, assign a plus or minus sign to every number so that the final sum equals target. Return the number of different ways to do it. This is a classic subset-sum transformation problem.

Input Format

nums = array of integers, target = desired final sum

Output Format

number of sign assignments that produce target

Constraints

  • 1 <= nums.length <= 20; 0 <= nums[i] <= 1000; -1000 <= target <= 1000

Examples

Example 1:

Input:

nums = [1,1,1,1,1]
target = 3

Output:

5

Explanation:

There are 5 ways to assign signs to get 3.

Example 2:

Input:

nums = [1]
target = 1

Output:

1

Explanation:

Only +1 works.

Loading...
Target Sum - Dp Knapsack DSA Problem