Open In App

Algorithms | Dynamic Programming | Question 7

Like Article
Like
Save Article
Save
Share
Report issue
Report

The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1 ,a2 ,a3 ,…,an} and positive integer W, is there a subset of S whose elements sum to W? A dynamic program for solving this problem uses a 2-dimensional Boolean array X, with n rows and W+1 columns. X[i, j],1 <= i <= n, 0 <= j <= W, is TRUE if and only if there is a subset of {a1 ,a2 ,…,ai} whose elements sum to j. Which of the following is valid for 2 <= i <= n and ai <= j <= W?

(A)

X[i, j] = X[i – 1, j] ∨ X[i, j -ai]

(B)

X[i, j] = X[i – 1, j] ∨ X[i – 1, j – ai]

(C)

X[i, j] = X[i – 1, j] ∧ X[i, j – ai]

(D)

X[i, j] = X[i – 1, j] ∧ X[i -1, j – ai]



Answer: (B)

Explanation:

X[I, j] (2 <= i <= n and ai <= j <= W), is true if any of the following is true 1) Sum of weights excluding ai is equal to j, i.e., if X[i-1, j] is true. 2) Sum of weights including ai is equal to j, i.e., if X[i-1, j-ai] is true so that we get (j – ai) + ai as j See http://www.geeksforgeeks.org/dynamic-programming-subset-sum-problem/ for details.

Hence (B) is the correct answer.


Quiz of this Question
Please comment below if you find anything wrong in the above post


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads