Target Sum
Medium
Topics
Given an array nums and an integer target k, assign a + or - to each number and count the assignments whose sum equals k.
Example 1
Each value gets a
+ or a -. Call the positive group’s sum P: then P - (total - P) = 3, so P = (total + target) / 2 = 4. That turns a sign-choice search into plain subset-sum counting, giving 5 ways.Cells are the array values; the small numbers underneath are indices.
Input: nums = [1,1,1,1,1], k = 3 Output: 5
Example 2
Input: nums = [1], k = 1 Output: 1
Constraints
- 1 <= nums.length <= 20
- 0 <= nums[i] <= 1000
- -1000 <= k <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
Group numbers into a positive set P and negative set N. Then sum(P) - sum(N) = k and sum(P) + sum(N) = total, so sum(P) = (total + k) / 2. Counting assignments becomes a subset-sum count.
Approach
- If (total + k) is odd or |k| > total, there are 0 ways.
- Let target = (total + k) / 2; count subsets summing to target with a 0/1-knapsack dp.
- dp[s] += dp[s - num] for each num.
Dry Run
nums = [1, 1, 1, 1, 1], k = 3
First the reduction. If P is the sum of the numbers given a plus sign, then
P - (total - P) = k, so P = (total + k) / 2. Here total = 5, so P = 8 / 2 = 4, and
the question becomes "how many subsets sum to 4?" -- an ordinary subset-count DP.
dp start [1, 0, 0, 0, 0]
after 1 [1, 1, 0, 0, 0]
after 1 [1, 2, 1, 0, 0]
after 1 [1, 3, 3, 1, 0]
after 1 [1, 4, 6, 4, 1]
after 1 [1, 5, 10, 10, 5]
answer dp[4] = 5
Those rows are Pascal's triangle, which is the sanity check: choosing a subset of
five equal 1s summing to 4 is C(5,4) = 5.
The inner loop runs BACKWARD, and that is the entire difference between 0/1 and
unbounded knapsack. Sweeping forward lets dp[s - num] already include the current
number, so one element gets reused -- this input then returns 70 instead of 5.
The parity and range guard is load-bearing, not defensive. With k = 2, total + k
is 7 and the halving would silently floor to 3, counting subsets that sum to 3 and
returning 10 where the answer is 0 -- no arrangement of five 1s sums to 2 at all,
because the parity is wrong.Complexity
- Time — O(n*sum): subset-sum dp over all numbers.
- Space — O(sum): the 1-D dp array.
Common Pitfalls
- Guard the parity and range of (total + k) before dividing by 2.
Full Solution — all languages
class Solution:
def findTargetSumWays(self, nums, k):
total=sum(nums)
if (total+k)%2 or abs(k)>total: return 0
target=(total+k)//2
dp=[0]*(target+1); dp[0]=1
for num in nums:
for s in range(target,num-1,-1):
dp[s]+=dp[s-num]
return dp[target]import java.util.*;
class Solution {
public int findTargetSumWays(int[] nums, int k) {
int total=0;
for(int x:nums)total+=x;
if((total+k)%2!=0||Math.abs(k)>total)return 0;
int target=(total+k)/2;
long[] dp=new long[target+1];
dp[0]=1;
for(int num:nums)for(int s=target;s>=num;s--)dp[s]+=dp[s-num];
return (int)dp[target];
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int findTargetSumWays(vector<int>& nums, int k) {
int total=0;
for(int x:nums)total+=x;
if((total+k)%2!=0||abs(k)>total)return 0;
int target=(total+k)/2;
vector<long long> dp(target+1,0);
dp[0]=1;
for(int num:nums)for(int s=target;s>=num;s--)dp[s]+=dp[s-num];
return (int)dp[target];
}
};/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/ var findTargetSumWays = function(nums, k) {
const total=nums.reduce((x,y)=>x+y,0);
if((total+k)%2!==0||Math.abs(k)>total)return 0;
const target=(total+k)/2,dp=new Array(target+1).fill(0);
dp[0]=1;
for(const num of nums)for(let s=target;s>=num;s--)dp[s]+=dp[s-num];
return dp[target];
};