I referred to the hint to this problem but the output is still coming 0.Please correct
0-N Knapsnack Problem
Hey @Yukta there were many small mistakes in your code
- size of arrays a, b would be greater than 1000
- while taking input of b use cin >> b[j] instead of b[value]
- in ans function dp size would be w+1
- you dont need to return anything when dp[i] != -1
- and your if condition is wrong the right if condition is if(a[j]<=i)dp[i]=max(dp[i],dp[i-a[j]] + b[j]);
Here is link of the code after rectification https://ide.codingblocks.com/s/176942
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.