Not able to find error in my code

vhttps://ide.codingblocks.com/s/649534

Hello @mohit26900,

I am listing down all the errors:

  1. Size of the array arr is w instead of n,

The second line contains W space-separated integers

  1. Size of the DP array should be w+1 instead of n.
  2. I think you misunderstood the array definition, array arr stores the cost of the ith packet and not the weight of the ith packet.
    Change this,

if(w-arr[i]>=0)
{
int sub=coins(n,w-arr[i],arr,dp);
ans=min(ans,sub+1);
}

to:

if(w-1-1>=0){
int sub=arr[i] + coins(n,w-i-1,arr,dp);
ans=min(ans,sub);
}

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.