Target Sum is LeetCode 494 (Medium). Put a + or a - in front of every number in nums and add everything up. Return how many sign choices give exactly target.
Every number gets a sign, including zeros; +0 and -0 count as different choices.
The numbers add up to at most 1,000, and that small total is what makes a table of sums cheap.
Split the numbers into the plus group, with sum P, and the minus group, with sum M. Then P − M = target and P + M = total. Adding the two equations gives
P = (total + target) / 2
So each valid sign choice is just a subset whose sum is (total + target) / 2: put + on its numbers and - on the rest. The task becomes counting subsets with a given sum, a 0/1 knapsack count.
If total + target is odd, or |target| > total, no whole-number P between 0 and total exists, and the answer is 0.
How to spot this pattern
Each item is either in or out (here, plus or minus) and you count the combinations that hit an exact total: that is a 0/1 knapsack count over sums.
03
Approach
Try it first
Before reading on, write the plus group's sum as P and the minus group's as M. Two facts link them to total and target. Solve for P and see what the question turns into. Aim for O(n · total).
Both run in O(n · total); the subset count wins because it needs one row of memory and no recursion. The steps, code and live demo follow the subset count, and the memoized search comes after the demo.
1
Rule out impossible targets
Return 0 if |target| > total or if total + target is odd. In either case the plus group would need a negative, too large or fractional sum.
2
Start from the empty subset
Set plus = (total + target) / 2 and make ways of length plus + 1 with ways[0] = 1. Before any number is used, exactly one subset, the empty one, has sum 0.
3
Add the numbers one at a time
For each number x, for every s from plus down to x, add ways[s − x] into ways[s]. Each subset that reached s − x without x reaches s once x joins it.
4
Read the answer
ways[plus] is the number of subsets with sum plus, and each one stands for exactly one sign choice.
04
Code in Python, C++ and Java
▶1class Solution:
▶2deffindTargetSumWays(self, nums, target):
▶3 total = sum(nums)
▶4ifabs(target) > total or (total + target) % 2:
▶5return0
▶6 plus = (total + target) // 2
▶7 ways = [1] + [0] * plus
▶8for x in nums:
▶9for s inrange(plus, x - 1, -1):
▶10 ways[s] += ways[s - x]
▶11return ways[plus]
▶1classSolution {
▶2public:
▶3intfindTargetSumWays(vector<int>& nums, int target) {
Call the sum of the numbers that get + the plus group and the rest the minus group. Plus minus minus must equal 0, and plus plus minus is the total, 4. Adding the two facts: twice the plus group equals 4 + 0.
plus2
ways[1, 0, 0]
Half of 4 is 2, so the question becomes: how many subsets of the numbers add up to 2? Each such subset is one sign choice. Before any number is used, only the empty subset exists, so sum 0 has 1 way and every other sum has 0.
x11st number
ways[2]0was 0
To reach 2 with this 1, the other numbers would need to make 1, and no subset of the numbers so far does. Sum 2 keeps 0.
x11st number
ways[1]1was 0
Every subset that made sum 0 without this 1 can add it to make 1. There is 1 such subset, so sum 1 goes from 0 to 1.
x22nd number
ways[2]1was 0
Every subset that made sum 0 without this 2 can add it to make 2. There is 1 such subset, so sum 2 goes from 0 to 1.
x13rd number
ways[2]2was 1
Every subset that made sum 1 without this 1 can add it to make 2. There is 1 such subset, so sum 2 goes from 1 to 2. Sum 1 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x13rd number
ways[1]2was 1
Every subset that made sum 0 without this 1 can add it to make 1. There is 1 such subset, so sum 1 goes from 1 to 2.
ways[2]2
All numbers are in. 2 subsets add up to 2, and each one is a sign choice: its numbers get +, the others get −, and the result is 0. The answer is 2.
total5
target3
Call the sum of the numbers that get + the plus group and the rest the minus group. Plus minus minus must equal 3, and plus plus minus is the total, 5. Adding the two facts: twice the plus group equals 5 + 3.
plus4
ways[1, 0, 0, 0, 0]
Half of 8 is 4, so the question becomes: how many subsets of the numbers add up to 4? Each such subset is one sign choice. Before any number is used, only the empty subset exists, so sum 0 has 1 way and every other sum has 0.
x11st number
ways[4]0was 0
To reach 4 with this 1, the other numbers would need to make 3, and no subset of the numbers so far does. Sum 4 keeps 0.
x11st number
ways[3]0was 0
To reach 3 with this 1, the other numbers would need to make 2, and no subset of the numbers so far does. Sum 3 keeps 0.
x11st number
ways[2]0was 0
To reach 2 with this 1, the other numbers would need to make 1, and no subset of the numbers so far does. Sum 2 keeps 0.
x11st number
ways[1]1was 0
Every subset that made sum 0 without this 1 can add it to make 1. There is 1 such subset, so sum 1 goes from 0 to 1.
x12nd number
ways[4]0was 0
To reach 4 with this 1, the other numbers would need to make 3, and no subset of the numbers so far does. Sum 4 keeps 0.
x12nd number
ways[3]0was 0
To reach 3 with this 1, the other numbers would need to make 2, and no subset of the numbers so far does. Sum 3 keeps 0.
x12nd number
ways[2]1was 0
Every subset that made sum 1 without this 1 can add it to make 2. There is 1 such subset, so sum 2 goes from 0 to 1. Sum 1 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x12nd number
ways[1]2was 1
Every subset that made sum 0 without this 1 can add it to make 1. There is 1 such subset, so sum 1 goes from 1 to 2.
x13rd number
ways[4]0was 0
To reach 4 with this 1, the other numbers would need to make 3, and no subset of the numbers so far does. Sum 4 keeps 0.
x13rd number
ways[3]1was 0
Every subset that made sum 2 without this 1 can add it to make 3. There is 1 such subset, so sum 3 goes from 0 to 1. Sum 2 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x13rd number
ways[2]3was 1
Every subset that made sum 1 without this 1 can add it to make 2. There are 2 such subsets, so sum 2 goes from 1 to 3. Sum 1 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x13rd number
ways[1]3was 2
Every subset that made sum 0 without this 1 can add it to make 1. There is 1 such subset, so sum 1 goes from 2 to 3.
x14th number
ways[4]1was 0
Every subset that made sum 3 without this 1 can add it to make 4. There is 1 such subset, so sum 4 goes from 0 to 1. Sum 3 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x14th number
ways[3]4was 1
Every subset that made sum 2 without this 1 can add it to make 3. There are 3 such subsets, so sum 3 goes from 1 to 4. Sum 2 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x14th number
ways[2]6was 3
Every subset that made sum 1 without this 1 can add it to make 2. There are 3 such subsets, so sum 2 goes from 3 to 6. Sum 1 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x14th number
ways[1]4was 3
Every subset that made sum 0 without this 1 can add it to make 1. There is 1 such subset, so sum 1 goes from 3 to 4.
x15th number
ways[4]5was 1
Every subset that made sum 3 without this 1 can add it to make 4. There are 4 such subsets, so sum 4 goes from 1 to 5. Sum 3 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x15th number
ways[3]10was 4
Every subset that made sum 2 without this 1 can add it to make 3. There are 6 such subsets, so sum 3 goes from 4 to 10. Sum 2 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x15th number
ways[2]10was 6
Every subset that made sum 1 without this 1 can add it to make 2. There are 4 such subsets, so sum 2 goes from 6 to 10. Sum 1 has not been updated for this number yet, because sums are visited from high to low, so it cannot count it twice.
x15th number
ways[1]5was 4
Every subset that made sum 0 without this 1 can add it to make 1. There is 1 such subset, so sum 1 goes from 4 to 5.
ways[4]5
All numbers are in. 5 subsets add up to 4, and each one is a sign choice: its numbers get +, the others get −, and the result is 3. The answer is 5.
total3
target2
Call the sum of the numbers that get + the plus group and the rest the minus group. Plus minus minus must equal 2, and plus plus minus is the total, 3. Adding the two facts: twice the plus group equals 3 + 2.
total + target5odd
answer0
Total plus target is 5, an odd number, so the plus group would need a sum of 2.5. Whole numbers never add up to a half, so no sign choice works. The answer is 0 without building any table.
05
Memoized search over signs
count(i, total) is the number of ways to finish from index i when the numbers before it add up to total. It tries +nums[i] and -nums[i] and caches each answer. The running sum stays between -total and total, so there are at most n × (2 · total + 1) states.
▶1from functools import cache
▶2
▶3class Solution:
▶4deffindTargetSumWays(self, nums, target):
▶5 @cache
▶6defcount(i, total):
▶7if i == len(nums):
▶8return1if total == target else0
▶9returncount(i + 1, total + nums[i]) + count(i + 1, total - nums[i])
▶10
▶11returncount(0, 0)
▶1classSolution {
▶2vector<vector<int>> memo;
▶3int offset, target;
▶4
▶5intcount(vector<int>& nums, int i, int total) {
▶6if (i == nums.size()) return total == target ? 1 : 0;
▶7int& seen = memo[i][total + offset];
▶8if (seen < 0) {
▶9 seen = count(nums, i + 1, total + nums[i]) + count(nums, i + 1, total - nums[i]);
▶10 }
▶11return seen;
▶12 }
▶13
▶14public:
▶15intfindTargetSumWays(vector<int>& nums, int target) {
▶5publicintfindTargetSumWays(int[] nums, int target) {
▶6for (int x : nums) offset += x;
▶7this.target = target;
▶8 memo = newint[nums.length][2 * offset + 1];
▶9for (int[] row : memo) Arrays.fill(row, -1);
▶10returncount(nums, 0, 0);
▶11 }
▶12
▶13privateintcount(int[] nums, int i, int total) {
▶14if (i == nums.length) return total == target ? 1 : 0;
▶15if (memo[i][total + offset] < 0) {
▶16 memo[i][total + offset] =
▶17count(nums, i + 1, total + nums[i]) + count(nums, i + 1, total - nums[i]);
▶18 }
▶19return memo[i][total + offset];
▶20 }
▶21}
06
Common pitfalls
Walking the sums upward
✗ Wrong
for s in range(x, plus + 1):
ways[s] += ways[s - x]
✓ Right
for s in range(plus, x - 1, -1):
ways[s] += ways[s - x]
Going up, ways[s − x] may already include x, so the same number is used again. For [1, 1, 1, 1, 1] and target 3 this returns 70 instead of 5. Going down reads only counts from before x was added.
Skipping the range check
✗ Wrong
if (total + target) % 2:
return 0
✓ Right
if abs(target) > total or (total + target) % 2:
return 0
For nums = [1] and target −3, plus is −1. In Python ways[-1] quietly reads the last cell and returns 1; in C++ or Java the array size is 0 and the code crashes. The answer is 0.
07
Edge cases
Zeros in nums, such as [0, 0, 1] with target 1
Each zero doubles the count, so the answer is 4. The loop handles it because with x = 0 it runs down to s = 0 and adds ways[s] to itself.
08
Complexity
Time
O(n · total)
Space
O(total)
plus is at most total, which is at most 1,000, so there are at most 20 × 1,001 updates. The ways array has plus + 1 cells.
09
Target Sum FAQ
How is this related to Partition Equal Subset Sum?
Partition Equal Subset Sum asks whether some subset reaches total / 2, a yes-or-no question. Here the subset must reach (total + target) / 2 and you count them all. The table is the same; booleans become counts.
Can the minus group be counted instead?
Yes. The minus group needs sum (total − target) / 2, and the count of those subsets is the same answer. When target is positive this sum is smaller, so the array is shorter.