As part of a problem, I need to create a recursive function on a string.
Part of it is based on the nth element of Fibonacci.
The thing is that I need to get n ( the number of elements ) as an input, and only then I can create the array.
But I also need to create a recursive function that has to use the array.
I thought of adding the array to the function as a parameter but I'm not sure if that would decrease the function's efficiency.
So in case it would: Is there any way I can use that specific array in a function in c++?
I calculate the array like this:
#include <bits/stdc++.h>
using namespace std;
char IOI(int n,int k) {
if (n<3) {
return n;
}
if (k<=fibo[n-2]) {
return IOI(n-2,k);
}
else {
return IOI(n-1,k-fibo[n-2]);
}
}
int main() {
int n,k;
cin >> n >> k;
int fibo[n+1];
fibo[0] = 0;
fibo[1] = 1;
for (int i = 2; i<n; i++) {
fibo[i] = fibo[i-1]+fibo[i-2];
}
cout << IOI(n,k)