To fix the issue you can do Your task is similiar to fibonnaci :) First i will explain you fibonacci.

code :

```
#include <cstdio>
#include <cstdlib>
using namespace std;
int f(int n) {
//printf("n = %d\n", n);
if(n == 1 || n == 2) // base case
return 1;
return f(n - 1) + f(n - 2); // recurrence relation
}
int main() {
int n; scanf("%d", &n);
printf("%d\n", f(n));
return 0;
}
```

```
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int N = 50;
long long memo[N];
long long f(int n) {
if(memo[n] != -1) // if we already computed the value of f(N), then return that value
return memo[n];
return memo[n] = f(n - 1) + f(n - 2); // else compute the value, and save it into the table
}
int main() {
memset(memo, -1, sizeof(memo));
memo[1] = memo[2] = 1; // add answer for base case to the table
int n; scanf("%d", &n);
printf("%lld\n", f(n));
return 0;
}
```

```
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int N = 25;
int check, memo[N];
int f(int x) {
if(memo[x] != check) // if f(n) was already computed
return memo[x]; // return computed value
return memo[x] = f(x - 2) - f(x - 1) + 2; // else compte given value and add it to a table
}
int main() {
memset(memo, 63, sizeof(memo)); // very big number, if the value of h(n) is different then that very big number, then we know we have computed the value for h(n)
check = memo[0];
memo[1] = 2; // base case
memo[2] = 3; // base case
int n; scanf("%d", &n);
printf("%d\n", f(n));
return 0;
}
```