Subset Sum Problem
Determine if there exists subset of given set with sum equal to target. Classic NP-complete problem solvable with DP in pseudo-polynomial O(n×sum) time. Variation of knapsack where all weights equal values.
Visualization
Interactive visualization for Subset Sum Problem
Subset Sum Problem
• Time: O(n × sum)
• Space: O(n × sum)
• Pseudo-polynomial time complexity
Interactive visualization with step-by-step execution
Implementation
1function subsetSum(nums: number[], target: number): boolean {
2 const dp: boolean[] = Array(target + 1).fill(false);
3 dp[0] = true;
4
5 for (const num of nums) {
6 for (let j = target; j >= num; j--) {
7 dp[j] = dp[j] || dp[j - num];
8 }
9 }
10
11 return dp[target];
12}
13
14// With subset reconstruction
15function findSubsetSum(nums: number[], target: number): number[] | null {
16 const n = nums.length;
17 const dp: boolean[][] = Array(n + 1).fill(0).map(() => Array(target + 1).fill(false));
18
19 for (let i = 0; i <= n; i++) {
20 dp[i][0] = true;
21 }
22
23 for (let i = 1; i <= n; i++) {
24 for (let j = 1; j <= target; j++) {
25 dp[i][j] = dp[i - 1][j];
26 if (j >= nums[i - 1]) {
27 dp[i][j] = dp[i][j] || dp[i - 1][j - nums[i - 1]];
28 }
29 }
30 }
31
32 if (!dp[n][target]) return null;
33
34 const subset: number[] = [];
35 let i = n, j = target;
36 while (i > 0 && j > 0) {
37 if (!dp[i - 1][j]) {
38 subset.push(nums[i - 1]);
39 j -= nums[i - 1];
40 }
41 i--;
42 }
43
44 return subset;
45}Complexity
Time
O(n × target)
O(n × target)
O(n × target)
Space
O(target)
Applications
Use Cases
Related Algorithms
0/1 Knapsack Problem
A classic optimization problem where you must select items with given weights and values to maximize total value without exceeding the knapsack's capacity. Each item can be taken only once (0 or 1). This is a fundamental problem in combinatorial optimization, resource allocation, and decision-making scenarios.
Longest Common Subsequence (LCS)
Finds the longest subsequence common to two sequences. A subsequence is a sequence that appears in the same relative order but not necessarily contiguously. This is fundamental in diff utilities, DNA sequence analysis, and version control systems like Git.
Edit Distance (Levenshtein Distance)
Edit Distance, also known as Levenshtein Distance, computes the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into another. Named after Soviet mathematician Vladimir Levenshtein who introduced it in 1965, this fundamental algorithm has applications in spell checking, DNA sequence analysis, natural language processing, and plagiarism detection.
Longest Increasing Subsequence (LIS)
Longest Increasing Subsequence (LIS) finds the length of the longest subsequence in an array where all elements are in strictly increasing order. Unlike a subarray, a subsequence doesn't need to be contiguous - elements can be selected from anywhere as long as their order is preserved. This classic DP problem has two solutions: O(n²) dynamic programming and O(n log n) binary search with patience sorting. Applications include stock price analysis, scheduling, Box Stacking Problem, and Building Bridges Problem.