Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
All possible binary numbers of length n with equal sum in both halves?
In this problem, we generate all possible binary numbers of length n where the sum of digits in the left half equals the sum of digits in the right half. For example, in the 6-bit number "100001", the left half "100" has sum 1 and the right half "001" also has sum 1, making them equal.
Syntax
void genAllBinEqualSumHalf(int n, char left[], char right[], int diff);
Algorithm
The algorithm uses recursion with backtracking −
- Base Case 1: When n = 0, if difference is 0, print the concatenated result
- Base Case 2: When n = 1 (odd length), place middle bit as both 0 and 1 if difference is 0
- Recursive Case: Try all four combinations (00, 01, 10, 11) for current positions in both halves
- Pruning: Only continue if it's possible to balance the remaining difference
Example
Here's the complete implementation to generate all binary numbers of length n with equal sum in both halves −
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void genAllBinEqualSumHalf(int n, char left[], char right[], int diff) {
if (n == 0) {
if (diff == 0) {
printf("%s%s ", left, right);
}
return;
}
if (n == 1) {
if (diff == 0) {
printf("%s0%s ", left, right);
printf("%s1%s ", left, right);
}
return;
}
if (2 * abs(diff) <= n) {
int len = strlen(left);
char newLeft[50], newRight[50];
/* Try all combinations except leading zero */
if (strlen(left) > 0) {
/* Case 1: Add 0 to both halves */
strcpy(newLeft, left);
strcat(newLeft, "0");
strcpy(newRight, right);
strcat(newRight, "0");
genAllBinEqualSumHalf(n-2, newLeft, newRight, diff);
/* Case 2: Add 0 to left, 1 to right */
strcpy(newLeft, left);
strcat(newLeft, "0");
strcpy(newRight, right);
strcat(newRight, "1");
genAllBinEqualSumHalf(n-2, newLeft, newRight, diff-1);
}
/* Case 3: Add 1 to left, 0 to right */
strcpy(newLeft, left);
strcat(newLeft, "1");
strcpy(newRight, right);
strcat(newRight, "0");
genAllBinEqualSumHalf(n-2, newLeft, newRight, diff+1);
/* Case 4: Add 1 to both halves */
strcpy(newLeft, left);
strcat(newLeft, "1");
strcpy(newRight, right);
strcat(newRight, "1");
genAllBinEqualSumHalf(n-2, newLeft, newRight, diff);
}
}
int main() {
int n = 6;
char left[50] = "";
char right[50] = "";
printf("Binary numbers of length %d with equal sum in both halves:<br>", n);
genAllBinEqualSumHalf(n, left, right, 0);
printf("<br>");
return 0;
}
Binary numbers of length 6 with equal sum in both halves: 100001 100010 101011 110011 100100 101101 101110 110101 110110 111111
How It Works
The algorithm builds binary strings from both ends simultaneously:
- diff parameter tracks the difference between left and right half sums
- When adding '1' to left half: diff increases by 1
- When adding '1' to right half: diff decreases by 1
- Pruning condition
2 * abs(diff) <= nensures we can balance the remaining positions - Leading zeros are avoided by checking if left string is non-empty
Conclusion
This recursive approach efficiently generates all binary numbers where both halves have equal digit sums. The pruning condition significantly reduces the search space by eliminating impossible branches early.
