Subset sum is knapsack
Updated
Subset sum is 0/1 knapsack with value=weight and a boolean table: can[w] = can[w] or can[w-a[i]] after considering item i. Target t is the capacity.
What to focus on
True at can[0] before any item (empty subset of a non-empty array). Iterate items outer, w from t down to a[i] so each item is used once. Unbounded subset-sum would scan w upward.
Worked examples
target 5
a = [1, 4, 9], t = 5- take 1 and 4
- can[5]=true
Result: true
target 6
a = [1, 4, 9], t = 6- no subset sums to 6
- 1+4=5, 9 too big, 1+9=10
Result: false
target 9
a = [1, 4, 9], t = 9- singleton 9
- can[9]=true
Result: true
Try it
Write subsetSum(a, t). a contains distinct positive integers. t is a positive integer. can[w] is true if some subset of a sums to w. Start from can[0]=true. Scan w downward. Return can[t]. false at t=6 is the algorithm's answer, not a missing array. t=5 → true. t=6 → false. t=9 → true.
a contains distinct positive integers. n = 3. t is a positive integer. false at t=6 is the algorithm's answer, not a missing array.