LADDER'S OPTIMISED APPROACH

Plz refer to the comments for doubt.

Hello @mverma_be19 what is the problem that you are facing?

I have commented it in the code.

Hello @mverma_be19 this is happening because first k terms we will not use the formula 2dp[n-1] -dp[n-k-1] because the indexing will go negative then.
that why for the first k terms we are using inly 2
dp[n-1].
and we are doing 2*dp[n-1] because if i am at the 6 the step lete say and k is 3 then i have to compute for 7th step. then i know the final answer for 6 th but i have not calculated them for 7th step that for 7th steo we have to add the value fo n-1th step and subtract the n-k-1 the step value becuse that is not the size of k window and that cannot contribute now.
so we have to subtract that from the n-1 th value so that we can get total value of in between steps ans as our size of the window is k-1 now thats why we have to add n-1th value to make window of size k.
if you have any doubt you can ask here:
Happy Learning!!

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.