Given a number n, we need to print all n-digit binary numbers with equal sum in left and right halves. If n is odd, then mid element can be either 0 or 1.
Input : n = 4 Output : 1001 1010 1111 Input : n = 5 Output : 10001 10101 10010 10110 11011 11111
The idea is to recursively build left and right halves and keep track of difference between counts of 1s in them. We print a string when difference becomes 0 and there are no more characters to add.
10001 10101 10010 10110 11011 11111
This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Length of the longest substring with equal 1s and 0s
- Binary String of given length that without a palindrome of size 3
- Check if a binary string contains all permutations of length k
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Number of Binary Strings of length N with K adjacent Set Bits
- Length of longest consecutive ones by at most one swap in a Binary String
- Generate Binary Strings of length N using Branch and Bound
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Find all even length binary sequences with same sum of first and second half bits
- Divide binary array into three equal parts with same value
- Split the binary string into substrings with equal number of 0s and 1s
- Print all increasing sequences of length k from first n natural numbers
- Longest subsequence having equal numbers of 0 and 1
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
Improved By : SURENDRA_GANGWAR