Count Number of Binary Strings Problem

How can we code this problem with help of recursion?

I have written this code:

#include<bits/stdc++.h>

using namespace std;

int fib(int n)

{

if(n<=2)
{
    return n;
}
return fib(n-1) + fib(n-2);

}

int main()

{

int t;
cin>>t;
while(t--)
{
    int n;
    cin>>n;
    cout<<fib(n);
}
return 0;

}

But it seems to be wrong.
Please help me with this

Hello @tanvesh i have corrected your code and added the dp part as you have to add dp in this:


if you have any doubt you can ask here;
Happy Learning!!

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.