Question: Suppose we are given a collection A={a_1,...,a_n} of n positive integers that add up to N. Design an O(nN) algorithm for determining whether there is a subset B\subset A such that \sum_{a_i\in B} a_i = \sum_{a_i\in A-B} a_i. Solution: We actually solve a more general problem. Assuming that N is the total sum of the numbers in A, for any given 0<= S <= N, we solve the problem of wether there is a subset of A, call A', such that the sum of numbers in A' is equal to S. Using this procedure, we can solve the original problem by just letting S=N/2 (assuming that N is even, and returning NO if N is odd). We use dynamic programming. For this problem, define table B[i,j] where 0<= i<= n and 0<= j <= N which holds 1 (True) or 0 (false) saying that there is a subset of the items in A_i={a_1,a_2,...,a_i} that adds to exactly j. Our solution will be B[n,S]. Clearly, B[0,0]=1, and for any i>0: B[0,i]=0 and B[i,0]=1 (by picking nothing). For any i>=1 and j>=1 we have: B[i,j]= max {B[i-1,j] , {B[i-1,j-a_i] (if j>= a_i) The first corresponds to the case that we find a subset with total j from items a_1,...,a_{i-1} and the second case corresponds to the case that we find a subset with total j-a_i from items a_1,...,a_{i-1} and then add item a_i to it. The running time is constant to compute every entry of table B and the size of the table is n*N so the time is O(n*N).