please check my code
Not getting the output
Hey @shivamgoel150, there are two issues with the code
-
The dp array should be of size n+1, since you’re accessing the dp[n] element while accessing the maximum_profit function for the first time.
-
The loop in the maximum_profit function should be initialized from 0 (there may not be any rod cutting), also the recursive function should call maximum_profit(n-i-1), (I encourage dry running it on pen and paper for better understanding).
for(ll i=0;i<n;i++)
{
ll profit=a[i]+maximum_profit(a,n-i-1,dp);
best=max(best,profit);
}
you can check the code here for your reference.