Please check this code

Hey @sehrawat See input format carefully, you have to take array input of size W not of n. and take care of cases when array value is -1 as told in the input format. I am attaching my code, you can take it for reference
Bottom up


Top down

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.