Generate Binary Strings with No Consecutive 1s

Given an integer K, your task is to produce all binary strings of length 'K' that do not contain consecutive '1's.

Input:

The input begins with an integer 'T', the number of test cases.

For each test case:
- A single integer 'K' is given, representing the desired length of the binary strings.

Output:

For each test case, output all possible binary strings of length 'K' with no consecutive '1's, in lexicographically increasing order.

Example:

Input:
T = 1
K = 3
Output:
000 001 010 100 101

Constraints:

  • 1 ≤ T ≤ 10
  • 0 ≤ K ≤ 22
  • Time limit: 1 second

Note:

  • Each string must consist solely of '0's and '1's.
  • No string should contain consecutive '1's.
  • The output should be in an array format, sorted in lexicographically increasing order.
AnswerBot
1d

Generate all binary strings of length 'K' with no consecutive '1's in lexicographically increasing order.

  • Use backtracking to generate all possible binary strings without consecutive '1's.

  • Start with an...read more

Help your peers!
Add answer anonymously...
Paytm Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter