You are given an integer ‘N’, you need to find the count of binary strings of length equal to ‘N’ that do not contain consecutive 1’s. As the answer can be large, print its value modulo 10^9 + 7.
Follow up :Can you try to solve this problem in O(N) time and O(1) space?
Example :
If ‘N’ = 3
Then there are 5 possible binary strings of length equal to 3 that do not contain consecutive 1’s: “000”, “001”, “010”, “100”, “101”.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’, denoting the length of the binary string(s).
Output Format :
For each test case, print the count of possible binary strings of length equal to ‘N’ that do not contain consecutive 1’s.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
1 ≤ N ≤ 20000
Time limit: 1 sec
2
3
1
5
2
For test case 1 :
We will print 5 because:
There are 5 possible binary strings of length equal to three that do not contain consecutive 1’s: “000”, “001”, “010”, “100” and “101”.
For test case 2 :
We will print 2 because:
There are 2 possible binary strings of length equal to one that does not contain consecutive 1’s: “0” and “1”.
2
4
100
8
470199269
Try to think of a bottom-up approach, ie: placing the i’th bit when you have already calculated the answer for i-1’th bit.
There are 2^N binary strings possible of length equal to N, we can recursively generate all possible binary strings, we can even prune this search by using a standard backtracking technique and generate only the strings that satisfy the given condition, but in this approach, we will discuss something that is bottom-up in nature and can later be extended to memoize results and become an efficient dynamic programming solution, but for this solution, we will limit our discussion to just a simple recursion.
For each of the i’th positions, we only have two possible bits to place as this is a binary string. We are allowed to place 1 at the i’th position only when i-1’th position doesn’t contain a 1, in other words, the number of strings that will be formed by placing 1 at the i’th position is equal to the number of strings formed by placing 0 at the i-1’th position. Further, the number of strings formed by placing 0 at the i’th position is equal to the number of strings formed by placing either a 0 or a 1 at the i-1’th position. This creates a perfect recursive approach as we are recalculating things in a similar way for each of the positions in the binary string.
The steps are as follows :
O( 2 ^ N ), where N denotes the length of the binary string.
To calculate the current position we are making two recursive calls to the previous position, this leads to an exponential time complexity (consider the number of nodes in a binary tree of height equal to ‘N’ for reference).
Hence the time complexity is O( 2^N ).
O( 1 )
No extra space is used.
Hence the space complexity is O( 1 ).