#include
using namespace std;
long long f(int n){
long long 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;
}
Giving segmentation fault and saying wrong answer while submitting
it is giving wrong answer because you have not print the answer according to question’s requirement
expected output
#1 : 2
#2 : 3
#3 : 5
your output
2
3
5
still its giving wrong ans #include using namespace std; long long f(int n){ long long 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<<"#"<<n<<":"<<f(n)<<endl; } return 0; }
why you are using 2D-dp here
it is just a fibo sequence
you can also solve it using 1D dp
like this
int main() {
int T;
cin>>T;
long long arr[45];
arr[0]=1;
arr[1]=2;
for(int i=2;i<45;i++)
arr[i]=arr[i-1]+arr[i-2];
for(int i=1;i<=T;i++)
{
cout<<"#"<<i<<" : ";
int N;
cin>>N;
cout<<arr[N]<<endl;
}
return 0;
}
in video hint was given by taking 2D dp array .So could u tell the error in that code itself and the code u gave is not understandable
please share the link of your code
it is not readable
paste your code at ide.codingblocks.com
and then file->save-> then link will be generated
at url
send it to me
so that i can check and run your code
cout<<"#"<<n<<" : "<<f(n)<<endl;
here you have to print i not n
we are taking n in input then where did i come from in main function? can u send proper corrected and tested code plz
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.