Given a integer K. Task is Print All binary string of size K (Given number).

Examples:

Input : K = 3 Output : 000 , 001 , 010 , 100 , 101 Input : K = 4 Output :0000 0001 0010 0100 0101 1000 1001 1010

Idea behind that is IF string ends with ‘1’ then we put only ‘0’ at the end. IF string ends with ‘0’ then we put both ‘0’ and ‘1’ at the end of string for generating new string.

Below is algorithm

K : size of string First We Generate All string starts with '0' initialize n = 1 . GenerateALLString ( K , Str , n ) a. IF n == K PRINT str. b. IF previous character is '1' :: str[n-1] == '1' put str[n] = '0' GenerateAllString ( K , str , n+1 ) c. IF previous character is '0' :: str[n-1] == '0' First We Put zero at end and call function PUT str[n] = '0' GenerateAllString ( K , str , n+1 ) PUT str[n] = '1' GenerateAllString ( K , str , n+1 ) Second Generate all binary string starts with '1' DO THE SAME PROCESS

Below is recursive implementation.

## C++

`// C++ program to Generate all binary string without ` `// consecutive 1's of size K ` `#include<bits/stdc++.h> ` `using` `namespace` `std ; ` ` ` `// A utility function generate all string without ` `// consecutive 1'sof size K ` `void` `generateAllStringsUtil(` `int` `K, ` `char` `str[], ` `int` `n) ` `{ ` ` ` `// print binary string without consecutive 1's ` ` ` `if` `(n == K) ` ` ` `{ ` ` ` `// terminate binary string ` ` ` `str[n] = ` `'\0'` `; ` ` ` `cout << str << ` `" "` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// if previous character is '1' then we put ` ` ` `// only 0 at end of string ` ` ` `//example str = "01" then new string be "010" ` ` ` `if` `(str[n-1] == ` `'1'` `) ` ` ` `{ ` ` ` `str[n] = ` `'0'` `; ` ` ` `generateAllStringsUtil (K , str , n+1); ` ` ` `} ` ` ` ` ` `// if previous character is '0' than we put ` ` ` `// both '1' and '0' at end of string ` ` ` `// example str = "00" then new string "001" and "000" ` ` ` `if` `(str[n-1] == ` `'0'` `) ` ` ` `{ ` ` ` `str[n] = ` `'0'` `; ` ` ` `generateAllStringsUtil(K, str, n+1); ` ` ` `str[n] = ` `'1'` `; ` ` ` `generateAllStringsUtil(K, str, n+1) ; ` ` ` `} ` `} ` ` ` `// function generate all binary string without ` `// consecutive 1's ` `void` `generateAllStrings(` `int` `K ) ` `{ ` ` ` `// Base case ` ` ` `if` `(K <= 0) ` ` ` `return` `; ` ` ` ` ` `// One by one stores every binary string of length K ` ` ` `char` `str[K]; ` ` ` ` ` `// Generate all Binary string starts with '0' ` ` ` `str[0] = ` `'0'` `; ` ` ` `generateAllStringsUtil ( K , str , 1 ) ; ` ` ` ` ` `// Generate all Binary string starts with '1' ` ` ` `str[0] = ` `'1'` `; ` ` ` `generateAllStringsUtil ( K , str , 1 ); ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `K = 3; ` ` ` `generateAllStrings (K) ; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to Generate all binary string ` `# without consecutive 1's of size K ` ` ` `# A utility function generate all string without ` `# consecutive 1'sof size K ` `def` `generateAllStringsUtil(K, ` `str` `, n): ` ` ` ` ` `# print binary string without consecutive 1's ` ` ` `if` `(n ` `=` `=` `K): ` ` ` ` ` `# terminate binary string ` ` ` `print` `(` `*` `str` `[:n], sep ` `=` `"` `", end = "` `") ` ` ` `return` ` ` ` ` `# if previous character is '1' then we put ` ` ` `# only 0 at end of string ` ` ` `# example str = "01" then new string be "000" ` ` ` `if` `(` `str` `[n` `-` `1` `] ` `=` `=` `'1'` `): ` ` ` `str` `[n] ` `=` `'0'` ` ` `generateAllStringsUtil (K, ` `str` `, n ` `+` `1` `) ` ` ` ` ` `# if previous character is '0' than we put ` ` ` `# both '1' and '0' at end of string ` ` ` `# example str = "00" then new string "001" and "000" ` ` ` `if` `(` `str` `[n` `-` `1` `] ` `=` `=` `'0'` `): ` ` ` `str` `[n] ` `=` `'0'` ` ` `generateAllStringsUtil(K, ` `str` `, n ` `+` `1` `) ` ` ` `str` `[n] ` `=` `'1'` ` ` `generateAllStringsUtil(K, ` `str` `, n ` `+` `1` `) ` ` ` `# function generate all binary string without ` `# consecutive 1's ` `def` `generateAllStrings(K): ` ` ` ` ` `# Base case ` ` ` `if` `(K <` `=` `0` `): ` ` ` `return` ` ` ` ` `# One by one stores every binary string of length K ` ` ` `str` `=` `[` `0` `] ` `*` `K ` ` ` ` ` `# Generate all Binary string starts with '0' ` ` ` `str` `[` `0` `] ` `=` `'0'` ` ` `generateAllStringsUtil (K, ` `str` `, ` `1` `) ` ` ` ` ` `# Generate all Binary string starts with '1' ` ` ` `str` `[` `0` `] ` `=` `'1'` ` ` `generateAllStringsUtil (K, ` `str` `, ` `1` `) ` ` ` `# Driver code ` `K ` `=` `3` `generateAllStrings (K) ` ` ` `# This code is contributed by SHUBHAMSINGH10 ` |

*chevron_right*

*filter_none*

**Output:**

000 001 010 100 101

This article is contributed by **Nishant Singh **. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Generate all binary strings from given pattern
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Strings formed from given characters without any consecutive repeating characters
- Generate Binary Strings of length N using Branch and Bound
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Generate a string which differs by only a single character from all given strings
- Print all Strings from array A[] having all strings from array B[] as subsequence
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Generate all the binary number from 0 to n
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
- Generate string with Hamming Distance as half of the hamming distance between strings A and B
- Generate a string from an array of alphanumeric strings based on given conditions
- Generate a String from given Strings P and Q based on the given conditions
- Print all strings of maximum length from an array of strings
- Count of sub-strings with equal consecutive 0's and 1's
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties