in the code the for loop has been started from 1, but shouldnt it be started from 0? The array starts from 0, and also for the first case (4, 0) we are cutting the second piece of length 0 only, i dont understand how this case would be computed if the for loop starts from 1. (because then we are always cutting a piece, and never considering the whole length)
Shouldnt the for loop start from 0
NO it must start from 1.
dp[len] depends on arr[cut]+dp[len-cut]
So when length is actually equal to to the cut length, this is also being considered her.
In the code, dp[0 ] was already initialized to 0.
Say if length of rod was 1.
You begin the next loop from 1.
So dp[1]=dp[1-1]+arr[1]=dp[0]+arr[1]=arr[1] This means that the rod is not being cut.
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.