Given a number n, find all binary sequences of length 2n such that sum of first n bits is same as sum of last n bits.
Input: N = 2 Output: 0101 1111 1001 0110 0000 1010 Input: N = 3 Output: 011011 001001 011101 010001 101011 111111 110011 101101 100001 110101 001010 011110 010010 001100 000000 010100 101110 100010 110110 100100
The idea is to fix first and last bits and then recur for remaining 2*(n-1) bits. There are four possibilities when we fix first and last bits –
- First and last bits are 1, remaining n – 1 bits on both sides should also have the same sum.
- First and last bits are 0, remaining n – 1 bits on both sides should also have the same sum.
- First bit is 1 and last bit is 0, sum of remaining n – 1 bits on left side should be 1 less than the sum n-1 bits on right side.
- First bit is 0 and last bit is 1, sum of remaining n – 1 bits on left side should be 1 more than the sum n-1 bits on right side.
Below is implementation of above idea –
0101 1111 1001 0110 0000 1010
This article is contributed by Aditya Goel. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Number of Binary Strings of length N with K adjacent Set Bits
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Print all increasing sequences of length k from first n natural numbers
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Count binary strings with twice zeros in first half
- Count sequences of length K having each term divisible by its preceding term
- Generate all the binary strings of N bits
- Count binary strings with k times appearing adjacent two set bits
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Maximum number of set bits count in a K-size substring of a Binary String
- Check if a binary string contains all permutations of length k
- Binary String of given length that without a palindrome of size 3
- Number of binary strings such that there is no substring of length ≥ 3
- Length of longest connected 1’s in a Binary Grid
- All possible binary numbers of length n with equal sum in both halves
- Count of binary strings of given length consisting of at least one 1
- Find the node whose sum with X has minimum set bits
- Find the node whose sum with X has maximum set bits
- Generate Binary Strings of length N using Branch and Bound