Why it is giving tle?

Hey @mb129162
Logic is correct, but constraints are tight and u are getting TLE because u applied top down

Apply Bottom up approach instead and it will work .

Yeah! Now it got accepted. Why is it not getting accepted in Top-Down?

Because of recursion overhead ,time to mantain stack of calls ,calling them etc.
Constraints should be very tight in this case

If this resolves ur query then please mark it as resolved :slight_smile: