Runs correctly on given input, but run error in higher testcases

Sir, I wrote the recursive solution and added memoization to it. The code runs correctly on smaller inputs. But it gives run error on higher cases.
I made a few checks to avoid going out of array bounds. But I am unable to realise the condition which I am missing. A prompt reply would be appreciable. Thanks in anticipation.

@nareshkumarmakwana1407 please attach a pic of ques


Sure here it is.

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.