The other simpler approach

Can’t we approach that way like find the number of bit permutations of n digit number having no consecutive ones
for 2 digit – 00 01 10
for 3 digit – 000 001 010 100 101
for 4 digit – 0000 0001 0010 0100 1000 1001 1010

for this question also

hi @arjun_121 yes you can do that as well. However you should also know the DP approach.

#include
using namespace std;

int main() {
int t;
int A[26];
A[0]=0;A[1] =2;A[2] = 3;
for(int i=3;i<26;i++){
A[i] = A[i-1]+A[i-2];
}
cin>>t;
while(t–){
int n;
cin>>n;
cout<<"#"<<n<<" : β€œ<<A[n]<<”\n";
}
return 0;
}

I have used that approach but that is giving me a wrong answer

@arjun_121 this is just bottom up DP, you have done nothing different. Also please share your code using CB IDE and explain the logic of this part

https://ide.geeksforgeeks.org/hJupcr8Xga

i have just tried to precompute the number of computations a N digit number formed from { 0 , 1} can have without consecutive ones.

and it follows a recurrence as f(n) = f(n-1) + f(n-2)

hi @arjun_121
your wrong answer is basically because of the output format. See the output format given in question, you have to print the answer only, nothing else.

The sample output shows as such
#1 : 2
#2 : 3
#3 : 5

and I have printed it the same way

@arjun_121 instead of β€œn” you have to print the case number.
like #1 for first test case, #2 for second and so on.

Thanks mam , I solved that

1 Like

@arjun_121 dont forget to mark your doubt as resolved!