Find all even length binary sequences with same sum of first and second half bits | Iterative
Last Updated :
30 Sep, 2022
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++
#include <iostream>
#include <malloc.h>
#include <math.h>
using namespace std;
void convertToBinary( int num, int a[], int n)
{
int pointer = n - 1;
while (num > 0) {
a[pointer] = num % 2;
num = num / 2;
pointer--;
}
}
void checkforsum( int a[], int n)
{
int sum1 = 0;
int sum2 = 0;
int mid = n / 2;
for ( int i = 0; i < mid; i++)
sum1 = sum1 + a[i];
for ( int j = mid; j < n; j++)
sum2 = sum2 + a[j];
if (sum1 == sum2) {
for ( int i = 0; i < n; i++)
cout << a[i];
cout << "\n" ;
}
}
void print_seq( int m)
{
int n = (2 * m);
int a[n];
for ( int j = 0; j < n; j++) {
a[j] = 0;
}
for ( int i = 0; i < ( int ) pow (2, n); i++) {
convertToBinary(i, a, n);
checkforsum(a, n);
}
}
int main()
{
int m = 2;
print_seq(m);
return 0;
}
|
Java
class GFG
{
static void convertToBinary( int num,
int a[], int n)
{
int pointer = n - 1 ;
while (num > 0 )
{
a[pointer] = num % 2 ;
num = num / 2 ;
pointer--;
}
}
static void checkforsum( int a[], int n)
{
int sum1 = 0 ;
int sum2 = 0 ;
int mid = n / 2 ;
for ( int i = 0 ; i < mid; i++)
sum1 = sum1 + a[i];
for ( int j = mid; j < n; j++)
sum2 = sum2 + a[j];
if (sum1 == sum2)
{
for ( int i = 0 ; i < n; i++)
System.out.print(a[i]);
System.out.println();
}
}
static void print_seq( int m)
{
int n = ( 2 * m);
int a[] = new int [n];
for ( int j = 0 ; j < n; j++)
{
a[j] = 0 ;
}
for ( int i = 0 ; i < ( int )Math.pow( 2 , n); i++)
{
convertToBinary(i, a, n);
checkforsum(a, n);
}
}
public static void main (String[] args)
{
int m = 2 ;
print_seq(m);
}
}
|
Python3
def convertToBinary(num, a, n):
pointer = n - 1
while (num > 0 ):
a[pointer] = num % 2
num = num / / 2
pointer - = 1
def checkforsum(a, n):
sum1 = 0
sum2 = 0
mid = n / / 2
for i in range (mid):
sum1 = sum1 + a[i]
for j in range (mid, n):
sum2 = sum2 + a[j]
if (sum1 = = sum2):
for i in range (n):
print (a[i], end = "")
print ()
def print_seq(m):
n = ( 2 * m)
a = [ 0 for i in range (n)]
for i in range ( pow ( 2 , n)):
convertToBinary(i, a, n)
checkforsum(a, n)
m = 2
print_seq(m)
|
C#
using System;
class GFG
{
static void convertToBinary( int num,
int []a, int n)
{
int pointer = n - 1;
while (num > 0)
{
a[pointer] = num % 2;
num = num / 2;
pointer--;
}
}
static void checkforsum( int []a, int n)
{
int sum1 = 0;
int sum2 = 0;
int mid = n / 2;
for ( int i = 0; i < mid; i++)
sum1 = sum1 + a[i];
for ( int j = mid; j < n; j++)
sum2 = sum2 + a[j];
if (sum1 == sum2)
{
for ( int i = 0; i < n; i++)
Console.Write(a[i]);
Console.WriteLine();
}
}
static void print_seq( int m)
{
int n = (2 * m);
int []a = new int [n];
for ( int j = 0; j < n; j++)
{
a[j] = 0;
}
for ( int i = 0; i < ( int )Math.Pow(2, n); i++)
{
convertToBinary(i, a, n);
checkforsum(a, n);
}
}
public static void Main (String[] args)
{
int m = 2;
print_seq(m);
}
}
|
Javascript
<script>
function convertToBinary(num, a, n)
{
let pointer = n - 1;
while (num > 0) {
a[pointer] = num % 2;
num = parseInt(num / 2);
pointer--;
}
}
function checkforsum(a, n)
{
let sum1 = 0;
let sum2 = 0;
let mid = parseInt(n / 2);
for (let i = 0; i < mid; i++)
sum1 = sum1 + a[i];
for (let j = mid; j < n; j++)
sum2 = sum2 + a[j];
if (sum1 == sum2) {
for (let i = 0; i < n; i++)
document.write(a[i]);
document.write( "<br>" );
}
}
function print_seq(m)
{
let n = (2 * m);
let a = new Array(n);
for (let j = 0; j < n; j++) {
a[j] = 0;
}
for (let i = 0; i < Math.pow(2, n); i++) {
convertToBinary(i, a, n);
checkforsum(a, n);
}
}
let m = 2;
print_seq(m);
</script>
|
Output:
0000
0101
0110
1001
1010
1111
Time Complexity: O(N*22N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...