#include
using namespace std;
int f(int n){
if(n<=1){
return 1;
}
return (f(n-1)+((n-1)*f(n-2)));
}
int main() {
int t;
int n;
for(int i=0;i<t;i++){
cin>>n;
cout<<f(n)<<endl;
}
return 0;
}
Getting TLE error in the code
Hey @mansi25
You havent inputted t
Thats why getting tle
Also use long long otherwise answer may overflow
#include<iostream>
using namespace std;
long long int solve(int n){
if(n<=2){
return n;
}
return solve(n-1)+((n-1)*solve(n-2));
}
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
cout<<solve(n)<<endl;
}
return 0;
}
this code is giving segmentation fault
Try with custom input
Its working perfectly on my side
I even submitted to check and its passing the given testcases