Take the 2-minute tour ×

I am facing difficulty in coming up with a solution for the problem given below:

We are given n boxes each having a weight ( it means each ball in box B_i have weight C_i),

Each box contain some balls specifically {b1,b2,b3...,b_n} (b_i is the count of balls in Box B_i).

we have to choose m balls out of it such that sum of the weights of m chosen balls be less than a given number T.

How many ways to do it?

share|improve this question
Hint: Suppose you build a solution by choosing k balls in order, with k at least 1. Suppose the first box you chose was i. Then the 2nd, 3rd, 4th, ..., kth balls must be a solution to a slight variation of the same problem, where the weight constraint is now T - C_i (instead of T), and b_i is one less than the original b_i. –  j_random_hacker Dec 26 '12 at 11:17
@j_random_hacker i got the recursive approach but facing difficulty in implementing it with using Dp, can you explain the Dp approach of yours. –  Akashdeep Saluja Dec 26 '12 at 12:33
Whenever you find a recursion that (a) solves the problem using only optimal solutions to the subproblems and (b) involves solving some subproblems more than once, you can use top-down DP (also called memoisation): whenever the function is called, check whether that subproblem has been solved before, and if so, simply return the answer right away. If not (i.e. if this is the first time this subproblem has been seen), solve the subproblem and store its result in the DP matrix (or other data structure). –  j_random_hacker Dec 29 '12 at 9:55
add comment

2 Answers

up vote 4 down vote accepted

First, let's have a look on a similar problem:

The similar problem is: you are looking to maximize the sum (such that it is still smaller then T), you are facing a variation of subset-sum problem, which is NP-Hard. The variation with a constant number of items is discussed in this thread: Sum-subset with a fixed subset size.

An alternative way to look at the problem is with a 2-dimensional knapsack problem, where weight = cost, and an extra dimension for number of elements. This concept is discussed in this thread: What's the fastest way to solve knapsack prob with two properties

Now, look at your problem: Finding the number of possible ways to achieve a sum which is smaller/equal T is still NP-Hard.

Assume you had a polynomial algorithm to do it, let it be A.

Running A(T) and A(T-1) will give you two numbers, if A(T) > A(T-1), the answer to the subset sum problem would have been true - otherwise it is false, so given a polynomial solution to this problem, we could prove P=NP.

share|improve this answer
its not an optimization prolem, i need to compute the number of possible ways to do so. –  Akashdeep Saluja Dec 26 '12 at 11:48
@AkashdeepSaluja: Oh, misunderstood you at first. I need to think for a minute, but I think it is still NP-Hard, since to find if there is any way to do it and get exactly to T is NP-Hard.. –  amit Dec 26 '12 at 11:52
not exactly to T, i just need the cases where sum of b_i is less than T –  Akashdeep Saluja Dec 26 '12 at 11:55
@AkashdeepSaluja: See edit, it is still NP-Hard. –  amit Dec 26 '12 at 11:55
@amit NP-hard means nothing. It's just the worst-case. For small inputs, an algorithm based on dynamic programming is still feasible. –  Xiao Jia Dec 26 '12 at 12:19
show 2 more comments

You can solve it by using dynamic programming techniques.

Let f[i][j][k] denote the number of ways to choose j balls from B_1 to B_i with sum of weights to be exactly k. The answer you want to get is f[n][m][T].

Initially, let f[i][j][k] = 1 for all i,j,k
for i = 1 to n
  for j = 0 to m
    for k = 0 to T
      for x = 0 to min(b_i,j)  # choose x balls from B_i
        y = x * C_i
        if y <= k
          f[i][j][k] = f[i][j][k] * f[i-1][j-x][k-y] * Comb(b_i,x)

Comb(n,k) is the number of ways to choose k elements from n elements.

The time complexity is O(n m T b) where b is the maximum number of balls in a box.

Note that, because of the T in the big-O notation, theoretically it is NP-hard. However, in practice, when T is relatively small, this algorithm is still feasible.

share|improve this answer
i don't want sum to be exactly T, sum may be less than T. –  Akashdeep Saluja Dec 26 '12 at 12:21
@AkashdeepSaluja Then sum up the values of f[n][m][z] where z = 0 to T –  Xiao Jia Dec 26 '12 at 12:22
add comment

Your Answer


