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
4mo

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!
Select
Add answer anonymously...

Paytm Software Engineer interview questions & answers

A Software Engineer was asked 7mo agoQ. Given an m x n matrix, where each row and each column is sorted in ascending ord...read more
A Software Engineer was asked 9mo agoQ. Explain the internal workings of a hashmap.
A Software Engineer was asked 9mo agoQ. Given head, the head of a linked list, determine if the linked list has a cycle ...read more

Popular interview questions of Software Engineer

A Software Engineer was asked 7mo agoQ1. Given an m x n matrix, where each row and each column is sorted in ascending ord...read more
A Software Engineer was asked 9mo agoQ2. Explain the internal workings of a hashmap.
A Software Engineer was asked 9mo agoQ3. Given head, the head of a linked list, determine if the linked list has a cycle ...read more
Paytm Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits