Algo-Sim › Dynamic Programming
Target Sum
Medium · Dynamic Programming · step through it in Java, Python and C++ on your own input
Put + or − before each number to hit a target. Algebra turns it into counting subsets with sum (total + target) / 2.
Problem
Given an integer array nums and an integer target, put a + or a - in front of every number and add them all up. Return the number of different sign choices whose result equals target.
Constraints
- 1 ≤ nums.length ≤ 20
- 0 ≤ nums[i] ≤ 1000
- 0 ≤ sum(nums) ≤ 1000
- -1000 ≤ target ≤ 1000
Edge cases to test
- (total + target) odd or |target| > total:
0 - Zeros double the count (+0 and −0)
- One number
Follow-up questions
- How does the algebra work?
- If P is the plus-set, P − (total − P) = target, so P = (total + target)/2: count subsets with that sum, O(n·sum).
- Is memoised DFS fine?
- Memoise on (index, running sum): O(n·sum) states, the same cost with more overhead.
Solution
Java
public int findTargetSumWays(int[] nums, int target) {
int total = 0;
for (int x : nums) total += x;
if (Math.abs(target) > total || (total + target) % 2 != 0) return 0;
int p = (total + target) / 2, n = nums.length;
int[][] dp = new int[n + 1][p + 1];
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int s = 0; s <= p; s++) {
dp[i][s] = dp[i - 1][s];
if (nums[i - 1] <= s) dp[i][s] += dp[i - 1][s - nums[i - 1]];
}
}
return dp[n][p];
}Python
def find_target_sum_ways(nums, target):
total = sum(nums)
if abs(target) > total or (total + target) % 2 != 0:
return 0
p, n = (total + target) // 2, len(nums)
dp = [[0] * (p + 1) for _ in range(n + 1)]
dp[0][0] = 1
for i in range(1, n + 1):
for s in range(p + 1):
dp[i][s] = dp[i - 1][s]
if nums[i - 1] <= s:
dp[i][s] += dp[i - 1][s - nums[i - 1]]
return dp[n][p]C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int findTargetSumWays(vector<int> &nums, int target) {
int total = 0;
for (int x : nums)
total += x;
if (abs(target) > total || (total + target) % 2 != 0)
return 0;
int p = (total + target) / 2, n = int(nums.size());
vector<vector<int>> dp = vector<vector<int>>(n + 1, vector<int>(p + 1));
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int s = 0; s <= p; s++) {
dp[i][s] = dp[i - 1][s];
if (nums[i - 1] <= s)
dp[i][s] += dp[i - 1][s - nums[i - 1]];
}
}
return dp[n][p];
}
};
More Dynamic Programming problems
- Climbing Stairs: Recursion → Memo → Table (Easy)
- Min Cost Climbing Stairs (Easy)
- House Robber (Medium)
- House Robber II (Medium)
- Coin Change (Medium)
- Coin Change II (Medium)
- Perfect Squares (Medium)
- Combination Sum IV (Medium)
- Decode Ways (Medium)
- Maximum Product Subarray (Medium)
- Best Time to Buy and Sell Stock with Cooldown (Medium)
- Best Time to Buy and Sell Stock with Transaction Fee (Medium)
- Best Time to Buy and Sell Stock III (Hard)
- Longest Increasing Subsequence (Medium)
- Word Break (Medium)
- Unique Paths (Medium)
- Unique Paths II (Medium)
- Minimum Path Sum (Medium)
- Maximal Square (Medium)
- Partition Equal Subset Sum (Medium)
- Longest Common Subsequence (Medium)
- Interleaving String (Medium)
- 0/1 Knapsack (Medium)
- Edit Distance (Medium)
- Distinct Subsequences (Hard)
- Longest Palindromic Substring (Medium)
- Palindromic Substrings (Medium)
- Longest Palindromic Subsequence (Medium)
- Predict the Winner (Medium)
- Stone Game (Medium)