Both your knapsack functions have issues. Both won't pass the simple test - S=1
n=1
s[0]=1
v[0]=10
. The answer should be 10
but your code will return 0
.
You can see that the issue are in the lines if (i==(n-1))
(knapsack1) or if (i==0)
(knapsack2).
Also, you will see issues because of condition if (j >= s[i]
. Your j
starts at 0
so, if S
was 1
, you'll never be able to the item with s[i] == 1
because j
will never reach 1
.
Hope this helps. Good luck.