Complete the code to initialize the DP array with zeros.
const dp: number[] = new Array(n + 1).fill([1]);
We fill the dp array with 0 because initially no subproblems are solved.
Complete the code to set the base case for the DP array.
dp[0] = [1];
Setting dp[0] = 1 means there is exactly one way to reach sum zero: by choosing nothing.
Fix the error in the loop condition to correctly iterate over sums.
for (let sum = 1; sum [1]= target; sum++) {
The loop must include the target sum, so use <= to cover all sums up to target.
Fill both blanks to update the DP array correctly inside the nested loop.
if (sum >= nums[i]) { dp[sum] = dp[sum] + dp[sum [1] nums[i]]; }
We subtract nums[i] from sum to find the previous state to add ways from.
Fill all three blanks to complete the tabulation function for subset sum count.
function countSubsets(nums: number[], target: number): number {
const dp: number[] = new Array(target + 1).fill([1]);
dp[0] = [2];
for (let i = 0; i < nums.length; i++) {
for (let sum = target; sum >= [3]; sum--) {
if (sum >= nums[i]) {
dp[sum] += dp[sum - nums[i]];
}
}
}
return dp[target];
}Initialize dp with 0, set dp[0] = 1 for base case, and iterate sums down from target to nums[i] to avoid reuse in the same iteration.