Coding interview problems โบ Dynamic Programming
๐ช Dynamic Programming Medium
Target Sum
Target Sum explained: turn the plus/minus sign problem into counting subsets with a fixed sum, then a 0/1 knapsack DP. Tested Python and an interactive visual.
Also searched as: target sum plus minus signs ยท assign + and - to reach target ยท count subsets with given difference
Try it live Free
0/1 Knapsack (DP)
In the Knapsack lab, watch every cell make a take-or-skip decision from the row above; in Target Sum, "take" means a plus sign and cells add up counts instead of values.
Also useful: Backtracking: Subsets, Permutations & Pruning
The problem
You are given a list of non-negative integers and a target. Put a + or - sign in front of every number. Count how many sign assignments make the expression equal target.
Exactly one number gets a minus sign, in any of 5 positions.
The key insight
Call the numbers with a plus sign P and the rest N. Then sum(P) - sum(N) = target and sum(P) + sum(N) = total, so sum(P) = (total + target) / 2. The question becomes how many subsets sum to that value, a counting 0/1 knapsack.
Step-by-step approach
- Compute
total. If|target| > totalortotal + targetis odd, return 0. - Set
need = (total + target) // 2. - Create
waysof sizeneed + 1withways[0] = 1. - For each number
x, loopsfromneeddown tox:ways[s] += ways[s - x]. - Return
ways[need]. Zeros are handled correctly: each zero doubles the count, matching +0 and -0.
Python solution
def find_target_sum_ways(nums, target):
total = sum(nums)
if abs(target) > total or (total + target) % 2:
return 0
need = (total + target) // 2 # sum of the plus-sign group
ways = [1] + [0] * need
for x in nums:
for s in range(need, x - 1, -1): # downward: each number once
ways[s] += ways[s - x]
return ways[need]Complexity
One sweep of a table of size about (total + target) / 2 per number.
Common mistakes
- Missing the parity and range checks, which leads to negative or fractional
need. - Mishandling zeros: each 0 can be +0 or -0, so it should double the count. The downward loop with
range(need, x - 1, -1)includess = needwhenx = 0and handles this. - Using a memo keyed only by index in the recursive version; the key must be (index, running sum).
Frequently asked questions
What is the time and space complexity of Target Sum?
The subset-sum DP is O(n ร S) time and O(S) space, where S is the sum of the numbers. The recursive version with memo on (index, sum) is O(n ร total) as well.
Why does Target Sum reduce to subset sum?
Splitting the numbers into a plus group P and a minus group N gives sum(P) - sum(N) = target and sum(P) + sum(N) = total. Adding the two equations fixes sum(P), so you only count subsets with that sum.
How do zeros affect the answer?
Each zero can take either sign without changing the sum, so it doubles the number of ways. The DP naturally handles this if the inner loop includes x = 0.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Dynamic Programming Coin Change (DP) lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.