Subset Sum Problem
Subset Sum Problem
URL:
SUBSET SUM PROBLEM
EXPLANATION
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
RECURSIVE SOLUTION
[value]
/ \
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 don’t. Whichever choice leads us to the target sum will be chosen.
SUBSET SUM PROBLEM
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
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
values[n] <= S
SUBSET SUM PROBLEM
RECURSIVE SOLUTION
Base condition
To identify the base condition of recursion, think about the smallest valid
items
SubSetSum(0, n) = true; for any value of n // We can obtain a zero sum by not
used in recursion.
SUBSET SUM PROBLEM
them recursively.
1. Initialization:
n is 0, return false.
2. Recursive Calls:
the memorized result and make a recursive call excluding the current
element.
Exclude the current element and make a recursive call with the
SUBSET SUM PROBLEM
3. Return Result:
4. Main Function:
Read input values for the number of elements `n`, the array of
`hasSubsetSumMemoized` 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
Time Complexity:
and targetSum is the target sum. This is due to the nested loops in the
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