Search⌘ K
AI Features

Subset Sum-Dynamic Programming

Explore how to solve the subset sum problem using dynamic programming in C++. Understand the memoization technique and iterative approach that reduce computation time compared to naive recursive methods. This lesson helps you implement efficient algorithms for checking if a subset sums to a target value.

Dynamic programming algorithm for subset sum problem

Recall that the SubsetSumSubsetSum problem asks whether any subset of a given array X[1..n]X [1 .. n] of positive integers sums to a given integer TT. In the previous lesson, we developed a recursive subset sum algorithm that can be reformulated as follows. Fix the original input array X[1..n]X [1 .. n] and define the boolean function

SS(i,t)=True if and only if some subset of X [i .. n] sums to t.SS(i, t) = True \text{ if and only if some subset of X [i .. n] sums to t.}

We need to compute SS(1,T)SS(1, T). This function satisfies the following recurrence:

SS(i,t)={Trueif t=0Falseif t<0 or i>n ...