# All possible binary numbers of length n with equal sum in both halves

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.

**Examples:**

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.

## C++

`// C++ program to generate all binary strings with ` `// equal sums in left and right halves. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Default values are used only in initial call. ` ` ` `n is number of bits remaining to be filled ` ` ` `di is current difference between sums of ` ` ` `left and right halves. ` ` ` `left and right are current half substrings. */` `void` `equal(` `int` `n, string left=` `""` `, string right=` `""` `, ` ` ` `int` `di=0) ` `{ ` ` ` `// TWO BASE CASES ` ` ` `// If there are no more characters to add (n is 0) ` ` ` `if` `(n == 0) ` ` ` `{ ` ` ` `// If difference between counts of 1s and ` ` ` `// 0s is 0 (di is 0) ` ` ` `if` `(di == 0) ` ` ` `cout << left + right << ` `" "` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `/* If 1 remains than string length was odd */` ` ` `if` `(n == 1) ` ` ` `{ ` ` ` `// If difference is 0, we can put remaining ` ` ` `// bit in middle. ` ` ` `if` `(di == 0) ` ` ` `{ ` ` ` `cout << left + ` `"0"` `+ right << ` `" "` `; ` ` ` `cout << left + ` `"1"` `+ right << ` `" "` `; ` ` ` `} ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `/* If difference is more than what can be ` ` ` `be covered with remaining n digits ` ` ` `(Note that lengths of left and right ` ` ` `must be same) */` ` ` `if` `((2 * ` `abs` `(di) <= n)) ` ` ` `{ ` ` ` ` ` `/*binary number would not start with 0*/` ` ` `if` `(left != ` `""` `) ` ` ` `{ ` ` ` `/* add 0 to end in both left and right ` ` ` `half. Sum in both half will not ` ` ` `change*/` ` ` `equal(n-2, left+` `"0"` `, right+` `"0"` `, di); ` ` ` ` ` `/* add 0 to end in both left and right ` ` ` `half* subtract 1 from di as right ` ` ` `sum is increased by 1*/` ` ` `equal(n-2, left+` `"0"` `, right+` `"1"` `, di-1); ` ` ` `} ` ` ` ` ` `/* add 1 in end in left half and 0 to the ` ` ` `right half. Add 1 to di as left sum is ` ` ` `increased by 1*/` ` ` `equal(n-2, left+` `"1"` `, right+` `"0"` `, di+1); ` ` ` ` ` `/* add 1 in end to both left and right ` ` ` `half the sum will not change*/` ` ` `equal(n-2, left+` `"1"` `, right+` `"1"` `, di); ` ` ` `} ` `} ` ` ` `/* driver function */` `int` `main() ` `{ ` ` ` `int` `n = 5; ` `// n-bits ` ` ` `equal(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to generate all binary strings ` `// with equal sums in left and right halves. ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Default values are used only in initial call. ` `// n is number of bits remaining to be filled ` `// di is current difference between sums of ` `// left and right halves. ` `// left and right are current half substrings. ` `static` `void` `equal(` `int` `n, String left, ` ` ` `String right, ` `int` `di) ` `{ ` ` ` `// TWO BASE CASES ` ` ` `// If there are no more characters to add (n is 0) ` ` ` `if` `(n == ` `0` `) ` ` ` `{ ` ` ` `// If difference between counts of 1s and ` ` ` `// 0s is 0 (di is 0) ` ` ` `if` `(di == ` `0` `) ` ` ` `System.out.print(left + right + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `/* If 1 remains than string length was odd */` ` ` `if` `(n == ` `1` `) ` ` ` `{ ` ` ` `// If difference is 0, we can put ` ` ` `// remaining bit in middle. ` ` ` `if` `(di == ` `0` `) ` ` ` `{ ` ` ` `System.out.print(left + ` `"0"` `+ right + ` `" "` `); ` ` ` `System.out.print(left + ` `"1"` `+ right + ` `" "` `); ` ` ` `} ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `/* If difference is more than what can be ` ` ` `be covered with remaining n digits ` ` ` `(Note that lengths of left and right ` ` ` `must be same) */` ` ` `if` `((` `2` `* Math.abs(di) <= n)) ` ` ` `{ ` ` ` ` ` `// binary number would not start with 0 ` ` ` `if` `(left != ` `""` `) ` ` ` `{ ` ` ` `// add 0 to end in both left and right ` ` ` `// half. Sum in both half will not ` ` ` `// change ` ` ` `equal(n - ` `2` `, left + ` `"0"` `, right + ` `"0"` `, di); ` ` ` ` ` `// add 0 to end in both left and right ` ` ` `// half* subtract 1 from di as right ` ` ` `// sum is increased by 1 ` ` ` `equal(n - ` `2` `, left + ` `"0"` `, right + ` `"1"` `, di - ` `1` `); ` ` ` `} ` ` ` ` ` `// add 1 in end in left half and 0 to the ` ` ` `// right half. Add 1 to di as left sum is ` ` ` `// increased by 1* ` ` ` `equal(n - ` `2` `, left + ` `"1"` `, right + ` `"0"` `, di + ` `1` `); ` ` ` ` ` `// add 1 in end to both left and right ` ` ` `// half the sum will not change ` ` ` `equal(n - ` `2` `, left + ` `"1"` `, right + ` `"1"` `, di); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` ` ` `// n-bits ` ` ` `equal(n, ` `""` `, ` `""` `, ` `0` `); ` `} ` `} ` ` ` `// This code is contributed ` `// by SURENDRA_GANGWAR ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to generate all binary strings with ` `# equal sums in left and right halves. ` ` ` `# Default values are used only in initial call. ` `# n is number of bits remaining to be filled ` `# di is current difference between sums of ` `# left and right halves. ` `# left and right are current half substrings. ` `def` `equal(n: ` `int` `, left ` `=` `"` `", right = "` `", di ` `=` `0` `): ` ` ` ` ` `# TWO BASE CASES ` ` ` `# If there are no more characters to add (n is 0) ` ` ` `if` `n ` `=` `=` `0` `: ` ` ` ` ` `# If difference between counts of 1s and ` ` ` `# 0s is 0 (di is 0) ` ` ` `if` `di ` `=` `=` `0` `: ` ` ` `print` `(left ` `+` `right, end ` `=` `" "` `) ` ` ` `return` ` ` ` ` `# If 1 remains than string length was odd ` ` ` `if` `n ` `=` `=` `1` `: ` ` ` ` ` `# If difference is 0, we can put remaining ` ` ` `# bit in middle. ` ` ` `if` `di ` `=` `=` `0` `: ` ` ` `print` `(left ` `+` `"0"` `+` `right, end ` `=` `" "` `) ` ` ` `print` `(left ` `+` `"1"` `+` `right, end ` `=` `" "` `) ` ` ` `return` ` ` ` ` `# If difference is more than what can be ` ` ` `# be covered with remaining n digits ` ` ` `# (Note that lengths of left and right ` ` ` `# must be same) ` ` ` `if` `2` `*` `abs` `(di) <` `=` `n: ` ` ` ` ` `# binary number would not start with 0 ` ` ` `if` `left !` `=` `"": ` ` ` ` ` `# add 0 to end in both left and right ` ` ` `# half. Sum in both half will not ` ` ` `# change ` ` ` `equal(n ` `-` `2` `, left ` `+` `"0"` `, right ` `+` `"0"` `, di) ` ` ` ` ` `# add 0 to end in both left and right ` ` ` `# half* subtract 1 from di as right ` ` ` `# sum is increased by 1 ` ` ` `equal(n ` `-` `2` `, left ` `+` `"0"` `, right ` `+` `"1"` `, di ` `-` `1` `) ` ` ` ` ` `# add 1 in end in left half and 0 to the ` ` ` `# right half. Add 1 to di as left sum is ` ` ` `# increased by 1 ` ` ` `equal(n ` `-` `2` `, left ` `+` `"1"` `, right ` `+` `"0"` `, di ` `+` `1` `) ` ` ` ` ` `# add 1 in end to both left and right ` ` ` `# half the sum will not change ` ` ` `equal(n ` `-` `2` `, left ` `+` `"1"` `, right ` `+` `"1"` `, di) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `5` `# n-bits ` ` ` `equal(` `5` `) ` ` ` `# This code is contributed by ` `# sanjeev2552 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to generate all binary strings ` `// with equal sums in left and right halves. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Default values are used only in initial call. ` `// n is number of bits remaining to be filled ` `// di is current difference between sums of ` `// left and right halves. ` `// left and right are current half substrings. ` `static` `void` `equal(` `int` `n, String left, ` ` ` `String right, ` `int` `di) ` `{ ` ` ` `// TWO BASE CASES ` ` ` `// If there are no more characters ` ` ` `// to add (n is 0) ` ` ` `if` `(n == 0) ` ` ` `{ ` ` ` `// If difference between counts of 1s ` ` ` `// and 0s is 0 (di is 0) ` ` ` `if` `(di == 0) ` ` ` `Console.Write(left + right + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `/* If 1 remains than string length was odd */` ` ` `if` `(n == 1) ` ` ` `{ ` ` ` `// If difference is 0, we can put ` ` ` `// remaining bit in middle. ` ` ` `if` `(di == 0) ` ` ` `{ ` ` ` `Console.Write(left + ` `"0"` `+ ` ` ` `right + ` `" "` `); ` ` ` `Console.Write(left + ` `"1"` `+ ` ` ` `right + ` `" "` `); ` ` ` `} ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `/* If difference is more than what can be ` ` ` `be covered with remaining n digits ` ` ` `(Note that lengths of left and right ` ` ` `must be same) */` ` ` `if` `((2 * Math.Abs(di) <= n)) ` ` ` `{ ` ` ` ` ` `// binary number would not start with 0 ` ` ` `if` `(left != ` `""` `) ` ` ` `{ ` ` ` `// add 0 to end in both left and right ` ` ` `// half. Sum in both half will not ` ` ` `// change ` ` ` `equal(n - 2, left + ` `"0"` `, right + ` `"0"` `, di); ` ` ` ` ` `// add 0 to end in both left and right ` ` ` `// half* subtract 1 from di as right ` ` ` `// sum is increased by 1 ` ` ` `equal(n - 2, left + ` `"0"` `, ` ` ` `right + ` `"1"` `, di - 1); ` ` ` `} ` ` ` ` ` `// add 1 in end in left half and 0 to the ` ` ` `// right half. Add 1 to di as left sum is ` ` ` `// increased by 1* ` ` ` `equal(n - 2, left + ` `"1"` `, ` ` ` `right + ` `"0"` `, di + 1); ` ` ` ` ` `// add 1 in end to both left and right ` ` ` `// half the sum will not change ` ` ` `equal(n - 2, left + ` `"1"` `, right + ` `"1"` `, di); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `// n-bits ` ` ` `equal(n, ` `""` `, ` `""` `, 0); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

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 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:

- 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
- Number of binary strings such that there is no substring of length ≥ 3
- Length of longest consecutive ones by at most one swap in a Binary String
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Find all even length binary sequences with same sum of first and second half bits
- Generate Binary Strings of length N using Branch and Bound
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Number of Binary Strings of length N with K adjacent Set Bits
- Divide binary array into three equal parts with same value
- Print all increasing sequences of length k from first n natural numbers
- Split the binary string into substrings with equal number of 0s and 1s