I tried solving the above program through recursion,
the purpose of this program was to count all possible distinct binary strings of length N such that there are no consecutive 1’s.
I got TLE error and tried to reslove but was not able to.
here is my code:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
unordered_map<ll,string>map1;
ll key=0;
void ones(ll n, string osf) {
if(n==0) {
map1[key]=osf;
key++;
return;
}
ones(n-1, osf+"0");
if(osf.size()==0 or osf[osf.size()-1]=='0') {
ones(n-1, osf+"1");
}
}
int main()
{
ll t, n,count=0;
cin>>t;
while(t!=0){
cin>>n;
ones(n, "");
for(ll i=0;i<map1.size();i++){
count++;
}
cout<<count<<endl;
map1.clear();
t–;
count=0;
}
}