VOOZH about

URL: https://www.geeksforgeeks.org/dsa/all-possible-binary-numbers-of-length-n-with-equal-sum-in-both-halves/

⇱ All possible binary numbers of length n with equal sum in both halves - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 30 Oct, 2022

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 : 0000 0101 0110 1001 1010 1111 
Input : n = 5
Output : 00000 00100 01001 01101 01010 01110 10001 10101 10010 10110 11011 11111 

The idea is to recursively build left and right halves and keep track of the difference between counts of 1s in them. We print a string when the difference becomes 0 and there are no more characters to add.  


Output
00000 00100 01001 01101 01010 01110 10001 10101 10010 10110 11011 11111 


Comment