Run time error on test cases

#include<bits/stdc++.h>
using namespace std;

int ansdp(int n){
if(n==0){
return 1;
}

int a[n];
int b[n];
a[0] = b[0] = 1;

for(int i=1;i<n;i++){

  a[i] = b[i-1];
  b[i] = b[i-1] +a[i-1];

}

return a[n-1] + b[n-1];

}

int main() {
int t;
cin>>t;
for(int i=0;i<t;i++){
int n;
cin>>n;
return ansdp(n);
}
}

here a[i] represents all strings ending with 1 and b[i] represents all strings ending with 0.

Hi @Mohitpandey29
in ur int function instead of using “return ansdp(n)”,
use cout<<ansdp(n)<<endl since once u use return statement in main() function, the int main() function is exited and ur code terminates but since value of t>1, it gives run time error. so try not to use return ansdp(n) in main and try using cout<<ansdp(n)<<endl.

Hope dis resolves ur doubt.

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.