Given a integer K. Task is Print All binary string of size K (Given number).
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.
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 email@example.com. 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.