My code is giving correct output but showing run error

your logic is fine but you are doing a lot of redundant work which increases the overall complexity of your code…try optimizing your code…
HINT:generate the ans for n you will see some similarity with fibonacci(n)