Professional Documents
Culture Documents
Subset Sum Problem
Subset Sum Problem
URL:
SUBSET SUM PROBLEM
EXPLANATION
and mathematics. Given a set of positive integers and a target sum, the goal
EXAMPLE
Output: True
EXAMPLE
Output: False
APPROACHES
1. Recursive Solution
RECURSIVE SOLUTION
In each step, the function calls itself with reduced input, and the
This approach often leads to concise and elegant code but may suffer from
RECURSIVE SOLUTION
[value]
/ \
value > targetSum value <= targetSumional space reserved. Only Stack
RECURSIVE SOLUTION
if value > targetSum; We can’t include this item in the subset. We don’t have a
choice
if value <= targetSum; We have a choice to either include this item in the subset or
RECURSIVE SOLUTION
Recursive structure
For every item, we have two choices - either include this item in the subset or don’t. We will
consider both the choices and determine if we can find a subset of sum S using any of the
choices or not.
we can find a subset of sum S using values from index 1..n-1 (Excluding the nth item)
or
we can find a subset of sum S-values[i] using values from index 1..n-1 (Including the nth item)
SUBSET SUM PROBLEM
RECURSIVE SOLUTION
If the value of the nth item is greater than the target sum S then we can’t include this
item in the subset and choice 1 is the only possibility. Let’s write our recursive
structure mathematically:
RECURSIVE SOLUTION
Base condition
To identify the base condition of recursion, think about the smallest valid input (Empty
SubsetSum(S, 0) = false; for S > 0 // We can't obtain a positive sum with no items
SubSetSum(0, n) = true; for any value of n // We can obtain a zero sum by not choosing
Space Complexity: O(1), no additional space reserved. Only Stack space is used in
recursion.
SUBSET SUM PROBLEM
Dynamic Programming Top-Down solution for the Subset Sum problem involves
recursively.
set of numbers.
SUBSET SUM PROBLEM
1. Initialization:
0, return false.
2. Recursive Calls:
If the current element's value is greater than the targetSum, set the
memorized result and make a recursive call excluding the current element.
Include the current element and make a recursive call with reduced
Exclude the current element and make a recursive call with the
3. Return Result:
4. Main Function:
Read input values for the number of elements `n`, the array of values,
function.
main():
n = read_input()
values = read_input_array()
target = read_input()
initialize subsetSumMem array
print "Has Subset sum: " + hasSubsetSumMemoized(values, target, n)
SUBSET SUM PROBLEM
import java.util.Scanner;
public class SubsetSumTopDown {
private static Boolean[][] subsetSumMem;
private static boolean hasSubsetSumRecur(int[] values, int targetSum, int n) {
if (targetSum == 0) {
return true;
}
if (n == 0) {
return false;
}
if (subsetSumMem[targetSum][n] != null) {
return subsetSumMem[targetSum][n];
}
if (values[n - 1] > targetSum) {
subsetSumMem[targetSum][n] = hasSubsetSumRecur(values, targetSum, n - 1);
} else {
subsetSumMem[targetSum][n] = hasSubsetSumRecur(values, targetSum - values[n - 1], n - 1)
|| hasSubsetSumRecur(values, targetSum, n - 1);
}
return subsetSumMem[targetSum][n];
}
SUBSET SUM PROBLEM
private static boolean hasSubsetSumMemoized(int[] values, int targetSum, int n) {
subsetSumMem = new Boolean[targetSum + 1][n + 1];
for (int s = 0; s <= targetSum; s++) {
for (int i = 0; i <= n; i++) {
subsetSumMem[s][i] = null;
}
}
return hasSubsetSumRecur(values, targetSum, n);
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int n = keyboard.nextInt();
int[] values = new int[n];
for (int i = 0; i < n; i++) {
values[i] = keyboard.nextInt();
}
int target = keyboard.nextInt();
keyboard.close();
System.out.println("Has Subset sum: " + hasSubsetSumMemoized(values, target, n));
}
}
SUBSET SUM PROBLEM
Time Complexity:
The time complexity is O(n * targetSum), where n is the number of elements and
targetSum is the target sum. This is due to the nested loops in the main function
Space Complexity:
memoization array.
INTERVIEW QUESTION
2. How does the dynamic programming approach solve the Subset Sum
problem?
3. Can you briefly describe the difference between the top-down and
bottom-up approaches for Subset Sum?
https://learn.codemithra.com