/*
*-----------------------------------------------------------*
| |
| |
| AUTHOR: Himanshu Aswal |
| (himanshu010) |
| |
| |
*-----------------------------------------------------------*
*/
#include<bits/stdc++.h>
#define moduli 998244353
#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
using namespace std;
int sum = 0;
void _printnum(int pos, int n, int one) {
static char str[100];
if (pos == n) {
// cout << str << '\n';
sum++;
return;
}
if (one == 1) {
str[pos] = '0';
_printnum(pos + 1, n, one - 1);
}
else {
str[pos] = '0';
_printnum(pos + 1, n, one);
str[pos] = '1';
_printnum(pos + 1, n, one + 1);
}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base:: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t; cin >> t; while (t--)
{
int i, j, k, n, m, ans = 0, cnt = 0;
cin >> n;
_printnum(0, n, 0);
cout << sum << endl;
sum = 0;
}
}
Code giving tle in some cases
hello @himanshu_aswal`,
you are getting tle because this recursive approach has exponential time complexity.
the recurrence relation for this problem will be->
t(n)=t(n-1) + t(n-2)
try using dynamic programming to optimise it furthur `
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.
I am not able to understand this by this condition can you give me the code so i can understand
check this ->