Question: Given a number n , consider all 2 ^ n sequences of a s and b s that have length n . A sequence is

Given a number n, consider all 2^n sequences of as and bs that have length n. A sequence is called a good one if and only if there are no two bs in a row (i.e., no two consecutive bs) anywhere in it. You have to count the total number N (n) of good sequences of length n. Implement an algorithm to solve this problem that runs in O(n). Explain why your algorithm yields the correct solution.
For example, if n =4, the following sequences are good: aaaa, aaab, aaba, abaa, baaa, abab, baab, baba : which yields the answer N (4)=8. Note, that you do not need to print all good sequences; the only thing you have to do is to find the number N (n).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!