Binary strings with no consecutive 1s.
You have been given an integer K.
Your task is to generate all binary strings of length K such that there are no consecutive 1s in the string. This means that the binary string should not contain any instance of 1’s coming one after another.
A binary string is that string which contains only ‘0’ and ‘1’.
For Example:
Let ‘K=3’, hence the length of the binary string would be 3. We can have the following binary strings with no consecutive 1s:
000 001 010 100 101
Note
1. Each string must be a binary string.
2. There should be no consecutive ‘1’ in the string.
3. Return an array/sequence of all the strings in an array in a lexicographically increasing order.
Input format:
The first line of input contains ‘T’ the number of test cases.
The first and only line of each test case contains a single integer K denoting the length of the binary string to be generated.
Output Format
For each test case, print all possible binary strings without consecutive ‘1’ of the length K in lexicographically increasing order.
Note:
You don't need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
0 <= K <= 22
Time limit: 1 second
CodingNinjas
author
2y
You can easily observe pattern. It is a Fibonacci sequence
CodingNinjas
author
2y
Recursive Approach
Since we need to generate all substring which does not have consecutive 1s we can simply start adding new characters to the string until the length of the string is ‘K’ taking care t...read more
Help your peers!
Add answer anonymously...
Top Paytm Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in Paytm Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app