3
$\begingroup$

Let set $S = \left \{ 1,2,3,4,5,6,\ldots,10,11,12 \right \}$.

Find how many sets are possible with a given sum and and upto given $m$.


For Ex : Take Sum = 6 and $m$ = size = $4$

The possible sets are : $\left \{ 2,4 \right \}, \left \{ 1,3,2 \right \}, \left \{ 1,4,1 \right \}, \left \{ 1,1,1,3 \right \}, \ldots$

but $\left \{ 1,1,1,1,2 \right \}$ is not valid as $m = 4$ and this contains $5$ elements


How many sets are possible with $m = 5$ and sum = $8$ ?

Is there any generalized idea to calculate sets ?

  • 0
    Hint: Suppose you have $k \leq m$ distinct boxes and $s$ identical balls. How many ways are there to place these balls in these boxes so that no box remains empty?2017-01-17

1 Answers 1

1

The problem is similar to Subset Sum Problem(http://www.geeksforgeeks.org/dynamic-programming-subset-sum-problem/) except you have one extra constraint of m.

After finding the all the sets with sum N, we can apply constraint on the size of m.

  • 2
    This is a very generalized algorithm. Here we have a special case that $S = \{1,2,3,...n\}$. Try making use of this information.2017-01-17
  • 0
    OOhh .. my bad... as question is explained using an example.. I thought it is a general set.. but I don't think it deserve a down vote...2017-01-17
  • 0
    Nope, I didn't.2017-01-17