494. Target Sum
Asked at Pinterest
Difficulty
Medium
Time
O(n * goal)
Space
O(goal)
Pattern
Array
Companies
1
Problem
You get an array of integers nums and an integer target. For every number you must choose one sign, + or -, and write the numbers in order with those signs in front of them. This builds one expression.
Return how many different sign choices produce an expression whose value equals target. Two choices count as different if at least one number gets a different sign. The order of the numbers never changes.
The trick that makes this fast: let P be the numbers you give a + and N the numbers you give a -. Then sum(P) - sum(N) = target, and sum(P) + sum(N) = total (the sum of all numbers). Adding these gives sum(P) = (target + total) / 2. So the question becomes: how many subsets of nums add up to (target + total) / 2? If target + total is odd, or abs(target) > total, no subset can work and the answer is 0.
Pick a sign for every number
Constraints
- ●
1 <= nums.length <= 20 - ●
0 <= nums[i] <= 1000 - ●
0 <= sum(nums[i]) <= 1000 - ●
-1000 <= target <= 1000
Examples
nums = [1,1,1,1,1], target = 35nums = [1], target = 11Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Medium | View all Pinterest questions → |
How to Think About It
Brute force recursion tries both signs for every number: O(2^n). Memoizing on (index, runningSum) brings it down to O(n * sum).
Key insight: split the numbers into a positive set P and a negative set N. Then sum(P) - sum(N) = target and sum(P) + sum(N) = total, so sum(P) = (target + total) / 2.
So the question becomes: how many subsets sum to (target + total) / 2? If target + total is odd or abs(target) > total, the answer is 0.
Count subsets with a 1D DP: dp[s] = number of ways to reach sum s. For each number x, iterate s from high to low and do dp[s] += dp[s - x] — iterating downward prevents reusing x.
Walkthrough for [1,1,1,1,1], target 3: total 5, subset target (3 + 5) / 2 = 4. Number of ways to choose four 1s from five = 5.
Optimal Approach
Step 1: total = sum(nums). If abs(target) > total or (target + total) is odd, return 0.
Step 2: goal = (target + total) // 2, dp = [1] + [0] * goal.
Step 3: For each x in nums: for s from goal down to x: dp[s] += dp[s - x].
Step 4: Return dp[goal].
Zeros are handled naturally: each zero doubles the count because dp[s] += dp[s - 0].
Time: O(n * goal). Space: O(goal).
Turn it into a subset-sum count
What Trips People Up in Real Interviews
Iterating the inner loop upward. That lets a number be used more than once — it becomes an unbounded knapsack and overcounts.
Forgetting the parity and range checks. A negative or fractional goal means there is no solution.
Using a dp array indexed by the signed running sum without an offset — negative indices break.
Missing that zeros double the answer (+0 and -0 are different expressions). The DP handles it; a hand-rolled shortcut might not.
Solution Code
O(n * goal) timedef findTargetSumWays(nums, target):
total = sum(nums)
if abs(target) > total or (target + total) % 2:
return 0
goal = (target + total) // 2
dp = [1] + [0] * goal
for x in nums:
for s in range(goal, x - 1, -1):
dp[s] += dp[s - x]
return dp[goal]Frequently Asked Questions
What is the Target Sum problem?
Target Sum asks in how many ways you can put a `+` or `-` in front of each number so the expression equals a target. Brute force tries `2^n` sign patterns; the elegant solution rewrites it as a subset-sum count and solves it with a 1D knapsack DP.
How do you solve Target Sum?
The optimal approach runs in O(n * goal) time and O(goal) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Target Sum?
Target Sum is asked at Pinterest. It is a medium difficulty problem.
What are common mistakes on Target Sum?
- Iterating the inner loop upward. That lets a number be used more than once — it becomes an unbounded knapsack and overcounts.
- Forgetting the parity and range checks. A negative or fractional `goal` means there is no solution.
- Using a `dp` array indexed by the signed running sum without an offset — negative indices break.
- Missing that zeros double the answer (`+0` and `-0` are different expressions). The DP handles it; a hand-rolled shortcut might not.