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 : 0000 0101 0110 1001 1010 1111 Input : n = 5 Output : 00000 00100 01001 01101 01010 01110 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.
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.
- Maximum cost of splitting given Binary Tree into two halves
- Maximize XOR subsequence possible by equidistant elements from both ends
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Minimum length of Run Length Encoding possible by removing at most K characters from a given string
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Find all even length binary sequences with same sum of first and second half bits
- Print all possible strings of length k that can be formed from a set of n characters
- Smallest number containing all possible N length permutations using digits 0 to D
- Count of binary strings of length N having equal count of 0's and 1's
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Check if a binary string contains all permutations of length k
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Print all increasing sequences of length k from first n natural numbers
- Print all possible ways to write N as sum of two or more positive integers
- Count lexicographically increasing K-length strings possible from first N alphabets
- Count ways to generate N-length array with 0s, 1s, and 2s such that sum of all adjacent pairwise products is K
- Find all combinations of two equal sum subsequences