# Find all even length binary sequences with same sum of first and second half bits

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 = 2Output:0101 1111 1001 0110 0000 1010Input:N = 3Output: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 –

## C++

`// C++ program to print even length binary sequences ` `// whose sum of first and second half bits is same ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print even length binary sequences ` `// whose sum of first and second half bits is same ` ` ` `// diff --> difference between sums of first n bits ` `// and last n bits ` `// out --> output array ` `// start --> starting index ` `// end --> ending index ` `void` `findAllSequences(` `int` `diff, ` `char` `* out, ` `int` `start, ` `int` `end) ` `{ ` ` ` `// We can't cover difference of more than n with 2n bits ` ` ` `if` `(` `abs` `(diff) > (end - start + 1) / 2) ` ` ` `return` `; ` ` ` ` ` `// if all bits are filled ` ` ` `if` `(start > end) ` ` ` `{ ` ` ` `// if sum of first n bits and last n bits are same ` ` ` `if` `(diff == 0) ` ` ` `cout << out << ` `" "` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// fill first bit as 0 and last bit as 1 ` ` ` `out[start] = ` `'0'` `, out[end] = ` `'1'` `; ` ` ` `findAllSequences(diff + 1, out, start + 1, end - 1); ` ` ` ` ` `// fill first and last bits as 1 ` ` ` `out[start] = out[end] = ` `'1'` `; ` ` ` `findAllSequences(diff, out, start + 1, end - 1); ` ` ` ` ` `// fill first and last bits as 0 ` ` ` `out[start] = out[end] = ` `'0'` `; ` ` ` `findAllSequences(diff, out, start + 1, end - 1); ` ` ` ` ` `// fill first bit as 1 and last bit as 0 ` ` ` `out[start] = ` `'1'` `, out[end] = ` `'0'` `; ` ` ` `findAllSequences(diff - 1, out, start + 1, end - 1); ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `// input number ` ` ` `int` `n = 2; ` ` ` ` ` `// allocate string contaning 2*n characters ` ` ` `char` `out[2 * n + 1]; ` ` ` ` ` `// null terminate output array ` ` ` `out[2 * n] = ` `'\0'` `; ` ` ` ` ` `findAllSequences(0, out, 0, 2*n - 1); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print even length binary ` `// sequences whose sum of first and second ` `// half bits is same ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to print even length binary sequences ` ` ` `// whose sum of first and second half bits is same ` ` ` ` ` `// diff --> difference between sums of first n bits ` ` ` `// and last n bits ` ` ` `// out --> output array ` ` ` `// start --> starting index ` ` ` `// end --> ending index ` ` ` `static` `void` `findAllSequences(` `int` `diff, ` `char` `out[], ` ` ` `int` `start, ` `int` `end) ` ` ` `{ ` ` ` `// We can't cover difference of more ` ` ` `// than n with 2n bits ` ` ` `if` `(Math.abs(diff) > (end - start + ` `1` `) / ` `2` `) ` ` ` `return` `; ` ` ` ` ` `// if all bits are filled ` ` ` `if` `(start > end) ` ` ` `{ ` ` ` `// if sum of first n bits and ` ` ` `// last n bits are same ` ` ` `if` `(diff == ` `0` `) ` ` ` `{ ` ` ` `System.out.print(out); ` ` ` `System.out.print(` `" "` `); ` ` ` `} ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// fill first bit as 0 and last bit as 1 ` ` ` `out[start] = ` `'0'` `; ` ` ` `out[end] = ` `'1'` `; ` ` ` `findAllSequences(diff + ` `1` `, out, start + ` `1` `, end - ` `1` `); ` ` ` ` ` `// fill first and last bits as 1 ` ` ` `out[start] = out[end] = ` `'1'` `; ` ` ` `findAllSequences(diff, out, start + ` `1` `, end - ` `1` `); ` ` ` ` ` `// fill first and last bits as 0 ` ` ` `out[start] = out[end] = ` `'0'` `; ` ` ` `findAllSequences(diff, out, start + ` `1` `, end - ` `1` `); ` ` ` ` ` `// fill first bit as 1 and last bit as 0 ` ` ` `out[start] = ` `'1'` `; ` ` ` `out[end] = ` `'0'` `; ` ` ` `findAllSequences(diff - ` `1` `, out, start + ` `1` `, end - ` `1` `); ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `// input number ` ` ` `int` `n = ` `2` `; ` ` ` ` ` `// allocate string contaning 2*n characters ` ` ` `char` `[] out = ` `new` `char` `[` `2` `* n + ` `1` `]; ` ` ` ` ` `// null terminate output array ` ` ` `out[` `2` `* n] = ` `'\0'` `; ` ` ` ` ` `findAllSequences(` `0` `, out, ` `0` `, ` `2` `*n - ` `1` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Pramod Kumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print even length binary sequences ` `# whose sum of first and second half bits is same ` ` ` `# Function to print even length binary sequences ` `# whose sum of first and second half bits is same ` ` ` `# diff --> difference between sums of first n bits ` `# and last n bits ` `# out --> output array ` `# start --> starting index ` `# end --> ending index ` `def` `findAllSequences(diff, out, start, end): ` ` ` ` ` `# We can't cover difference of more than n with 2n bits ` ` ` `if` `(` `abs` `(diff) > (end ` `-` `start ` `+` `1` `) ` `/` `/` `2` `): ` ` ` `return` `; ` ` ` ` ` `# if all bits are filled ` ` ` `if` `(start > end): ` ` ` `# if sum of first n bits and last n bits are same ` ` ` `if` `(diff ` `=` `=` `0` `): ` ` ` `print` `(''.join(` `list` `(out)),end` `=` `" "` `); ` ` ` `return` `; ` ` ` ` ` `# fill first bit as 0 and last bit as 1 ` ` ` `out[start] ` `=` `'0'` `; ` ` ` `out[end] ` `=` `'1'` `; ` ` ` `findAllSequences(diff ` `+` `1` `, out, start ` `+` `1` `, end ` `-` `1` `); ` ` ` ` ` `# fill first and last bits as 1 ` ` ` `out[start] ` `=` `out[end] ` `=` `'1'` `; ` ` ` `findAllSequences(diff, out, start ` `+` `1` `, end ` `-` `1` `); ` ` ` ` ` `# fill first and last bits as 0 ` ` ` `out[start] ` `=` `out[end] ` `=` `'0'` `; ` ` ` `findAllSequences(diff, out, start ` `+` `1` `, end ` `-` `1` `); ` ` ` ` ` `# fill first bit as 1 and last bit as 0 ` ` ` `out[start] ` `=` `'1'` `; ` ` ` `out[end] ` `=` `'0'` `; ` ` ` `findAllSequences(diff ` `-` `1` `, out, start ` `+` `1` `, end ` `-` `1` `); ` ` ` `# Driver program ` ` ` `# input number ` `n ` `=` `2` `; ` ` ` `# allocate string contaning 2*n characters ` `out` `=` `[""]` `*` `(` `2` `*` `n); ` ` ` `findAllSequences(` `0` `, out, ` `0` `, ` `2` `*` `n ` `-` `1` `); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print even length binary ` `// sequences whose sum of first and second ` `// half bits is same ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to print even length binary ` ` ` `// sequences whose sum of first and ` ` ` `// second half bits is same ` ` ` ` ` `// diff --> difference between sums of ` ` ` `// first n bits ` ` ` `// and last n bits ` ` ` `// out --> output array ` ` ` `// start --> starting index ` ` ` `// end --> ending index ` ` ` `static` `void` `findAllSequences(` `int` `diff, ` ` ` `char` `[]outt, ` `int` `start, ` `int` `end) ` ` ` `{ ` ` ` ` ` `// We can't cover difference of ` ` ` `// more than n with 2n bits ` ` ` `if` `(Math.Abs(diff) > (end - start ` ` ` `+ 1) / 2) ` ` ` `return` `; ` ` ` ` ` `// if all bits are filled ` ` ` `if` `(start > end) ` ` ` `{ ` ` ` ` ` `// if sum of first n bits and ` ` ` `// last n bits are same ` ` ` `if` `(diff == 0) ` ` ` `{ ` ` ` `Console.Write(outt); ` ` ` `Console.Write(` `" "` `); ` ` ` `} ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// fill first bit as 0 and last bit ` ` ` `// as 1 ` ` ` `outt[start] = ` `'0'` `; ` ` ` `outt[end] = ` `'1'` `; ` ` ` `findAllSequences(diff + 1, outt, ` ` ` `start + 1, end - 1); ` ` ` ` ` `// fill first and last bits as 1 ` ` ` `outt[start] = outt[end] = ` `'1'` `; ` ` ` `findAllSequences(diff, outt, ` ` ` `start + 1, end - 1); ` ` ` ` ` `// fill first and last bits as 0 ` ` ` `outt[start] = outt[end] = ` `'0'` `; ` ` ` `findAllSequences(diff, outt, ` ` ` `start + 1, end - 1); ` ` ` ` ` `// fill first bit as 1 and last ` ` ` `// bit as 0 ` ` ` `outt[start] = ` `'1'` `; ` ` ` `outt[end] = ` `'0'` `; ` ` ` `findAllSequences(diff - 1, outt, ` ` ` `start + 1, end - 1); ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` ` ` `// input number ` ` ` `int` `n = 2; ` ` ` ` ` `// allocate string contaning 2*n ` ` ` `// characters ` ` ` `char` `[]outt = ` `new` `char` `[2 * n + 1]; ` ` ` ` ` `// null terminate output array ` ` ` `outt[2 * n] = ` `'\0'` `; ` ` ` ` ` `findAllSequences(0, outt, 0, 2*n - 1); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to print even length binary sequences ` `// whose sum of first and second half bits is same ` ` ` `// Function to print even length binary sequences ` `// whose sum of first and second half bits is same ` ` ` `// diff --> difference between sums of first n bits ` `// and last n bits ` `// out --> output array ` `// start --> starting index ` `// end --> ending index ` `function` `findAllSequences(` `$diff` `, ` `$out` `, ` `$start` `, ` `$end` `) ` `{ ` ` ` `// We can't cover difference of more than n with 2n bits ` ` ` `if` `(` `abs` `(` `$diff` `) > (int)((` `$end` `- ` `$start` `+ 1) / 2)) ` ` ` `return` `; ` ` ` ` ` `// if all bits are filled ` ` ` `if` `(` `$start` `> ` `$end` `) ` ` ` `{ ` ` ` `// if sum of first n bits and last n bits are same ` ` ` `if` `(` `$diff` `== 0) ` ` ` `print` `(implode(` `""` `,` `$out` `).` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// fill first bit as 0 and last bit as 1 ` ` ` `$out` `[` `$start` `] = ` `'0'` `; ` ` ` `$out` `[` `$end` `] = ` `'1'` `; ` ` ` `findAllSequences(` `$diff` `+ 1, ` `$out` `, ` `$start` `+ 1, ` `$end` `- 1); ` ` ` ` ` `// fill first and last bits as 1 ` ` ` `$out` `[` `$start` `] = ` `$out` `[` `$end` `] = ` `'1'` `; ` ` ` `findAllSequences(` `$diff` `, ` `$out` `, ` `$start` `+ 1, ` `$end` `- 1); ` ` ` ` ` `// fill first and last bits as 0 ` ` ` `$out` `[` `$start` `] = ` `$out` `[` `$end` `] = ` `'0'` `; ` ` ` `findAllSequences(` `$diff` `, ` `$out` `, ` `$start` `+ 1, ` `$end` `- 1); ` ` ` ` ` `// fill first bit as 1 and last bit as 0 ` ` ` `$out` `[` `$start` `] = ` `'1'` `; ` ` ` `$out` `[` `$end` `] = ` `'0'` `; ` ` ` `findAllSequences(` `$diff` `- 1, ` `$out` `, ` `$start` `+ 1, ` `$end` `- 1); ` `} ` ` ` `// Driver program ` ` ` ` ` `// input number ` ` ` `$n` `= 2; ` ` ` ` ` `// allocate string contaning 2*n characters ` ` ` `$out` `=` `array_fill` `(0,2*` `$n` `,` `""` `); ` ` ` ` ` `findAllSequences(0, ` `$out` `, 0, 2*` `$n` `- 1); ` ` ` `// This code is contributed by chandan_jnu ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

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 contribute@geeksforgeeks.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.

## Recommended Posts:

- 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
- Generate all the binary strings of N bits
- Count binary strings with k times appearing adjacent two set bits
- Binary String of given length that without a palindrome of size 3
- Check if a binary string contains all permutations of length k
- All possible binary numbers of length n with equal sum in both halves
- Length of longest consecutive ones by at most one swap in a Binary String
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Find the node whose sum with X has maximum set bits
- Find the node whose sum with X has minimum set bits
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K