What is wrong with this code?
Hello @vinaykumar4530,
You have missed an important part of the question:
When some packets are not available i.e. arr[i]=-1.
Solution:
-
Remove the packages that are not available:
1.1. use 2 vectors to store price and weight of available packets.
1.2. Code:
vector wt,val;
int size=0;
for(int i=0;i<n;i++)
{
if(cost[i]!=-1)
{
val.push_back(cost[i]);
wt.push_back(i+1);
size++;
}
} -
Use these vectors for processing:
dp[i][j]=min(dp[i-1][j],dp[i][j-wt[i-1]]+val[i-1]);
Hope, this would help.
Give a like, if you are satisfied.
but i have checked this condition in line 15??
Yes, i just checked.
The actual problem is with the input part of your program.
You are taking n prices.
But, in actual you have to take w prices i.e. price for each weight till w.
I have modified your code:
Hope, this would help.
Give a like, if you are satisfied.