Coded this in same way as it was shown in video but TLE error is coming

#include
using namespace std;
int f(int n){

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

}
int main() {
int t,n;
while(t–){
cin>>n;
cout<<f(n)<<endl;
}
return 0;
}

this approach of code is showing segmentation fault

#include using namespace std; int f(int n){ int dp[1000][1000]; dp[1][0]=1; dp[1][1]=1; for(int i=2;i<=n;i++){ dp[i][0]=dp[i-1][0]+dp[i-1][1]; dp[i][1]=dp[i-1][0]; } return (dp[n][0]+dp[n][1]); } int main() { int t,n; cin>>t; while(t–){ cin>>n; cout<<f(n)<<endl; } return 0; }

Hey @mansi25
Please share your code in Coding Blocks IDE :)’

how to share it via coding blocks ide?

collaborate mode is on .So u would be able to see it?

In this

#include<iostream>
using namespace std;
int f(int n){

	if (n<=2){
		return 1+n;
	}
	return (f(n-1)+f(n-2));
}
int main() {
	int t,n;
    cin>>t;//u forgot this
	while(t--){
		cin>>n;
		cout<<f(n)<<endl;
	}
	return 0;
}

Anyway it will give TLE because time complexity of this code is exponential

In this

#include<iostream>
using namespace std;
long long  f(int n){ //long long here 
	long long dp[100][100]; //long long here 
	dp[1][0]=1;
	dp[1][1]=1;
	for(int i=2;i<=n;i++){
		dp[i][0]=dp[i-1][0]+dp[i-1][1];
		dp[i][1]=dp[i-1][0];
	}
	return dp[n][0]+dp[n][1];
}
int main() {
	int t,n;
	cin>>t;
	while(t--){
		cin>>n;
		cout<<f(n)<<endl;
	}
	return 0;
}

Used long long to avoid overflow
Otherwise it’s not giving segmentation

Maybe u are running ur code without giving custom inputs in that case it assumes garbage input which may be out of range that’s why u are getting segmentation error

only 1 test case is geting passed out of 3 in your corrected code

in which one???
See recursion code will give u TLE

This one should work
I already checked :slight_smile: