Related Articles

Related Articles

Find all even length binary sequences with same sum of first and second half bits | Iterative
  • Difficulty Level : Medium
  • Last Updated : 24 Oct, 2019

Given a number N, find all binary sequences of length 2*N such that sum of first N bits is same as the sum of last N bits.

Examples:

Input: N = 2
Output:
0000
0101
0110
1001
1010
1111

Input: N = 1
Output:
00
11

Note: The recursive approach to this problem can be found here.
Approach:
A simple approach to run a loop from 0 to 22*N and convert into the binary form and check whether the sum of first half is equal to the sum of the second half.



If the above condition is true, then print that number, else check for the next one.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation
#include <iostream>
#include <malloc.h>
#include <math.h>
using namespace std;
  
// Function to convert the
// number into binary and
// store the number into
// an array
void convertToBinary(int num, int a[], int n)
{
  
    int pointer = n - 1;
    while (num > 0) {
        a[pointer] = num % 2;
        num = num / 2;
        pointer--;
    }
}
  
// Function to check if the
// sum of the digits till
// the mid of the array and
// the sum of the digits
// from mid till n is the
// same, if they are same
// then print that binary
void checkforsum(int a[], int n)
{
  
    int sum1 = 0;
    int sum2 = 0;
    int mid = n / 2;
  
    // Calculating the sum from
    // 0 till mid and store
    // in sum1
    for (int i = 0; i < mid; i++)
        sum1 = sum1 + a[i];
  
    // Calculating the sum
    // from mid till n and
    // store in sum2
    for (int j = mid; j < n; j++)
        sum2 = sum2 + a[j];
  
    // If sum1 is same as
    // sum2 print the binary
    if (sum1 == sum2) {
        for (int i = 0; i < n; i++)
            cout << a[i];
        cout << "\n";
    }
}
  
// Function to print sequence
void print_seq(int m)
{
  
    int n = (2 * m);
  
    // Creating the array
    int a[n];
  
    // Initialize the array
    // with 0 to store the
    // binary nnumbers
    for (int j = 0; j < n; j++) {
        a[j] = 0;
    }
  
    for (int i = 0; i < (int)pow(2, n); i++) {
  
        // Converting the number
        // into binary first
        convertToBinary(i, a, n);
  
        // Checking if the sum of
        // the first half of the
        // array is same as the
        // sum of the next half
        checkforsum(a, n);
    }
}
  
// Driver Code
int main()
{
    int m = 2;
  
    print_seq(m);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code implementation for above approach
class GFG
{
      
    // Function to convert the 
    // number into binary and 
    // store the number into 
    // an array 
    static void convertToBinary(int num,
                                int a[], int n) 
    
        int pointer = n - 1
        while (num > 0
        
            a[pointer] = num % 2
            num = num / 2
            pointer--; 
        
    
      
    // Function to check if the 
    // sum of the digits till 
    // the mid of the array and 
    // the sum of the digits 
    // from mid till n is the 
    // same, if they are same 
    // then print that binary 
    static void checkforsum(int a[], int n) 
    
        int sum1 = 0
        int sum2 = 0
        int mid = n / 2
      
        // Calculating the sum from 
        // 0 till mid and store 
        // in sum1 
        for (int i = 0; i < mid; i++) 
            sum1 = sum1 + a[i]; 
      
        // Calculating the sum 
        // from mid till n and 
        // store in sum2 
        for (int j = mid; j < n; j++) 
            sum2 = sum2 + a[j]; 
      
        // If sum1 is same as 
        // sum2 print the binary 
        if (sum1 == sum2)
        
            for (int i = 0; i < n; i++) 
                System.out.print(a[i]); 
            System.out.println(); 
        
    
      
    // Function to print sequence 
    static void print_seq(int m) 
    
      
        int n = (2 * m); 
      
        // Creating the array 
        int a[] = new int[n]; 
      
        // Initialize the array 
        // with 0 to store the 
        // binary nnumbers 
        for (int j = 0; j < n; j++) 
        
            a[j] = 0
        
      
        for (int i = 0; i < (int)Math.pow(2, n); i++) 
        
      
            // Converting the number 
            // into binary first 
            convertToBinary(i, a, n); 
      
            // Checking if the sum of 
            // the first half of the 
            // array is same as the 
            // sum of the next half 
            checkforsum(a, n); 
        
    
      
    // Driver Code 
    public static void main (String[] args)
    
        int m = 2
      
        print_seq(m); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of above approach
  
# Function to convert the number into binary 
# and store the number into an array
def convertToBinary(num, a, n):
  
    pointer = n - 1
    while (num > 0):
        a[pointer] = num % 2
        num = num // 2
        pointer -= 1
  
# Function to check if the sum of the digits till
# the mid of the array and the sum of the digits
# from mid till n is the same, if they are same
# then print that binary
def checkforsum(a, n):
  
    sum1 = 0
    sum2 = 0
    mid = n // 2
  
    # Calculating the sum from 0 till mid 
    # and store in sum1
    for i in range(mid):
        sum1 = sum1 + a[i]
  
    # Calculating the sum from mid till n 
    # and store in sum2
    for j in range(mid, n):
        sum2 = sum2 + a[j]
  
    # If sum1 is same as sum2 print the binary
    if (sum1 == sum2):
        for i in range(n):
            print(a[i], end = "")
        print()
  
# Function to prsequence
def print_seq(m):
  
    n = (2 * m)
  
    # Creating the array
    a = [0 for i in range(n)]
  
  
    for i in range(pow(2, n)):
  
        # Converting the number
        # into binary first
        convertToBinary(i, a, n)
  
        # Checking if the sum of the first half 
        # of the array is same as the sum of 
        # the next half
        checkforsum(a, n)
  
# Driver Code
m = 2
  
print_seq(m)
  
# This code is contributed by mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code implementation for above approach
using System;
      
class GFG
{
      
    // Function to convert the 
    // number into binary and 
    // store the number into 
    // an array 
    static void convertToBinary(int num,
                                int []a, int n) 
    
        int pointer = n - 1; 
        while (num > 0) 
        
            a[pointer] = num % 2; 
            num = num / 2; 
            pointer--; 
        
    
      
    // Function to check if the 
    // sum of the digits till 
    // the mid of the array and 
    // the sum of the digits 
    // from mid till n is the 
    // same, if they are same 
    // then print that binary 
    static void checkforsum(int []a, int n) 
    
        int sum1 = 0; 
        int sum2 = 0; 
        int mid = n / 2; 
      
        // Calculating the sum from 
        // 0 till mid and store 
        // in sum1 
        for (int i = 0; i < mid; i++) 
            sum1 = sum1 + a[i]; 
      
        // Calculating the sum 
        // from mid till n and 
        // store in sum2 
        for (int j = mid; j < n; j++) 
            sum2 = sum2 + a[j]; 
      
        // If sum1 is same as 
        // sum2 print the binary 
        if (sum1 == sum2)
        
            for (int i = 0; i < n; i++) 
                Console.Write(a[i]); 
            Console.WriteLine(); 
        
    
      
    // Function to print sequence 
    static void print_seq(int m) 
    
      
        int n = (2 * m); 
      
        // Creating the array 
        int []a = new int[n]; 
      
        // Initialize the array 
        // with 0 to store the 
        // binary nnumbers 
        for (int j = 0; j < n; j++) 
        
            a[j] = 0; 
        
      
        for (int i = 0; i < (int)Math.Pow(2, n); i++) 
        
      
            // Converting the number 
            // into binary first 
            convertToBinary(i, a, n); 
      
            // Checking if the sum of 
            // the first half of the 
            // array is same as the 
            // sum of the next half 
            checkforsum(a, n); 
        
    
      
    // Driver Code 
    public static void Main (String[] args)
    
        int m = 2; 
      
        print_seq(m); 
    
}
      
// This code is contributed by PrinciRaj1992

chevron_right


Output:

0000
0101
0110
1001
1010
1111

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :