I want to implement efficiently "streaming Knapsack" problem in java.

The problem is I have a stream input of integer data coming continuously for example -1, 2, 9, 5, 5, 11, 1 -3,...

The question is to find the first "k" elements in which their sum is "n>0". for example k=3 and n=12, then the solution is: ...,2,...,5, 5.

I found an answer in http://programmingpraxis.com/2012/05/15/streaming-knapsack/2/ as: (It is mainly for positive Integer Values)

But looking for simpler one! Any Ideas?

  • 2
    $\begingroup$ What do you mean by simpler ones? Faster ones? Ones requiring less memory? Ones easier to program? $\endgroup$ Jan 25, 2013 at 21:13
  • $\begingroup$ faster and simpler to code $\endgroup$
    – Reza
    Jan 25, 2013 at 22:20
  • 2
    $\begingroup$ I'm not sure that there is any faster simple solution (or any faster solution at all), and I don't think that this dynamic programming solution is that hard to code, even in Java. $\endgroup$ Jan 25, 2013 at 22:51
  • $\begingroup$ You meantion you get a "stream" of data. Does that impose any restrictions on the algorithm? For example, does it have unlimited memory? $\endgroup$
    – Raphael
    Jan 26, 2013 at 17:51
  • $\begingroup$ yes, it should be memory efficient. $\endgroup$
    – Reza
    Jan 26, 2013 at 18:58


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.