Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've just read this article about solving the below minimum subset sum using dynamic programming technique.

Given a list of $N$ coins, their values $(V_1, V_2, ... , V_N)$, and the total sum S. Find the minimum number of coins the sum of which is $S$ (we can use as many coins of one type as we want), or report that it's not possible to select coins in such a way that they sum up to $S$.

How can I use the same technique to find the maximum subset of coins that sums up to $S$? And what is the recurrence relation for this 2nd problem?

share|cite|improve this question
I think cs.SE is more appropriate. – Frank Science Jun 28 '12 at 7:04
up vote 0 down vote accepted

The pseudocode for minimum subset sum given in the article is as follows:

Set M[i] equal to infinity for all of i

For i = 1 to S
  For j = 0 to N - 1
    If (Vj<=i AND M[i-Vj]+1<M[i])
    Then M[i]=M[i-Vj]+1

Output M[S]

The inner loop over $j$ is just computing $M[i] := \min\limits_{V_j \le i} M[i-V_j] + 1$. To compute the maximum subset sum, you just have to replace $\min$ with $\max$, which you can do by initializing $M$ to $-\infty$ instead of $\infty$, and changing the less-than sign in the comparison of $M$ to a greater-than sign.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.