Find all even length binary sequences with same sum of first and second half bits - GeeksforGeeks
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.
Examples:
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
We strongly recommend you to minimize your browser and try this yourself first.
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.
Read full article from Find all even length binary sequences with same sum of first and second half bits - GeeksforGeeks
No comments:
Post a Comment