Posted on by Kalkicode
Code Backtracking

Find all even length binary sequences with same sum of first and second half bits

Here given code implementation process.

``````// C Program
// Find all even length binary sequences with same
// sum of first and second half bits
#include <stdio.h>

// Display calculated result
void printResult(int result[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", result[i]);
}
printf("\n");
}
void subSequences(int result[], int start,
int index,
int difference,
int n)
{
if (index == n / 2)
{
if (difference == 0)
{
printResult(result, n);
}
}
else if (index >= n || difference > n / 2 ||
difference < -(n / 2))
{
return;
}
else
{
// Set 0 in first and second part same index position
result[index] = 0;
result[(n / 2) + index] = 0;
subSequences(result, start + 1, index + 1, difference, n);
// Set 1 in first and second part index position
result[index] = 1;
result[(n / 2) + index] = 1;
subSequences(result, start + 1, index + 1, difference, n);
// Set 0 in first part and 1 in second part index position
result[index] = 0;
result[(n / 2) + index] = 1;
subSequences(result, start + 1, index + 1, difference + 1, n);
// Set 1 in first part and 0 in second part index position
result[index] = 1;
result[(n / 2) + index] = 0;
subSequences(result, start + 1, index + 1, difference - 1, n);
}
}
void equalDifferentParts(int n)
{
if (n <= 0 || n % 2 != 0)
{
return;
}
// Auxiliary array which is collect result
int result[n];
subSequences(result, 0, 0, 0, n);
}
int main()
{
// Length of binary sequences
int n = 6;
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]

[0 0 1 ] [0 0 1 ]

[0 1 0 ] [0 1 0 ]

[0 1 1 ] [0 1 1 ]

[0 0 1 ] [0 1 0 ]

[0 1 0 ] [0 0 1 ]

[1 0 0 ] [1 0 0 ]

[1 0 1 ] [1 0 1 ]

[1 1 0 ] [1 1 0 ]

[1 1 1 ] [1 1 1 ]

[1 0 1 ] [1 1 0 ]

[1 1 0 ] [1 0 1 ]

[0 0 1 ] [1 0 0 ]

[0 1 1 ] [1 1 0 ]

[0 1 0 ] [1 0 0 ]

[0 1 1 ] [1 0 1 ]

[1 0 0 ] [0 0 1 ]

[1 1 0 ] [0 1 1 ]

[1 0 0 ] [0 1 0 ]

[1 0 1 ] [0 1 1 ]

-----------------
Pair with same number of active bits
*/
equalDifferentParts(n);
return 0;
}``````

Output

``````0 0 0 0 0 0
0 0 1 0 0 1
0 1 0 0 1 0
0 1 1 0 1 1
0 0 1 0 1 0
0 1 0 0 0 1
1 0 0 1 0 0
1 0 1 1 0 1
1 1 0 1 1 0
1 1 1 1 1 1
1 0 1 1 1 0
1 1 0 1 0 1
0 0 1 1 0 0
0 1 1 1 1 0
0 1 0 1 0 0
0 1 1 1 0 1
1 0 0 0 0 1
1 1 0 0 1 1
1 0 0 0 1 0
1 0 1 0 1 1``````
``````// Java program for
// Print all combinations of n natural number
// whose pair element difference is one
public class Combination
{
// Display calculated result
public void printResult(int[] result, int n)
{
for (int i = 0; i < n; i++)
{
System.out.print("  " + result[i]);
}
System.out.print("\n");
}
public void subSequences(int[] result,
int start,
int index,
int difference,
int n)
{
if (index == n / 2)
{
if (difference == 0)
{
printResult(result, n);
}
}
else if (index >= n ||
difference > n / 2 ||
difference < -(n / 2))
{
return;
}
else
{
// Set 0 in first and second part same index position
result[index] = 0;
result[(n / 2) + index] = 0;
subSequences(result, start + 1,
index + 1, difference, n);
// Set 1 in first and second part index position
result[index] = 1;
result[(n / 2) + index] = 1;
subSequences(result, start + 1,
index + 1, difference, n);
// Set 0 in first part and 1 in second part index position
result[index] = 0;
result[(n / 2) + index] = 1;
subSequences(result, start + 1,
index + 1, difference + 1, n);
// Set 1 in first part and 0 in second part index position
result[index] = 1;
result[(n / 2) + index] = 0;
subSequences(result, start + 1,
index + 1, difference - 1, n);
}
}
public void equalDifferentParts(int n)
{
if (n <= 0 || n % 2 != 0)
{
return;
}
// Auxiliary array which is collect result
int[] result = new int[n];
subSequences(result, 0, 0, 0, n);
}
public static void main(String[] args)
{
// Length of binary sequences
int n = 6;
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]

[0 0 1 ] [0 0 1 ]

[0 1 0 ] [0 1 0 ]

[0 1 1 ] [0 1 1 ]

[0 0 1 ] [0 1 0 ]

[0 1 0 ] [0 0 1 ]

[1 0 0 ] [1 0 0 ]

[1 0 1 ] [1 0 1 ]

[1 1 0 ] [1 1 0 ]

[1 1 1 ] [1 1 1 ]

[1 0 1 ] [1 1 0 ]

[1 1 0 ] [1 0 1 ]

[0 0 1 ] [1 0 0 ]

[0 1 1 ] [1 1 0 ]

[0 1 0 ] [1 0 0 ]

[0 1 1 ] [1 0 1 ]

[1 0 0 ] [0 0 1 ]

[1 1 0 ] [0 1 1 ]

[1 0 0 ] [0 1 0 ]

[1 0 1 ] [0 1 1 ]

-----------------
Pair with same number of active bits
*/
}
}``````

Output

``````  0  0  0  0  0  0
0  0  1  0  0  1
0  1  0  0  1  0
0  1  1  0  1  1
0  0  1  0  1  0
0  1  0  0  0  1
1  0  0  1  0  0
1  0  1  1  0  1
1  1  0  1  1  0
1  1  1  1  1  1
1  0  1  1  1  0
1  1  0  1  0  1
0  0  1  1  0  0
0  1  1  1  1  0
0  1  0  1  0  0
0  1  1  1  0  1
1  0  0  0  0  1
1  1  0  0  1  1
1  0  0  0  1  0
1  0  1  0  1  1``````
``````// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Print all combinations of n natural number
// whose pair element difference is one
class Combination
{
public:
// Display calculated result
void printResult(int result[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "  " << result[i];
}
cout << "\n";
}
void subSequences(int result[], int start,
int index,
int difference,
int n)
{
if (index == n / 2)
{
if (difference == 0)
{
this->printResult(result, n);
}
}
else if (index >= n ||
difference > n / 2 ||
difference < -(n / 2))
{
return;
}
else
{
// Set 0 in first and second part same index position
result[index] = 0;
result[(n / 2) + index] = 0;
this->subSequences(result,
start + 1,
index + 1,
difference, n);
// Set 1 in first and second part index position
result[index] = 1;
result[(n / 2) + index] = 1;
this->subSequences(result,
start + 1,
index + 1,
difference, n);
// Set 0 in first part and 1 in second part index position
result[index] = 0;
result[(n / 2) + index] = 1;
this->subSequences(result,
start + 1,
index + 1,
difference + 1, n);
// Set 1 in first part and 0 in second part index position
result[index] = 1;
result[(n / 2) + index] = 0;
this->subSequences(result,
start + 1,
index + 1,
difference - 1, n);
}
}
void equalDifferentParts(int n)
{
if (n <= 0 || n % 2 != 0)
{
return;
}
// Auxiliary array which is collect result
int result[n];
this->subSequences(result, 0, 0, 0, n);
}
};
int main()
{
// Length of binary sequences
int n = 6;
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]
[0 0 1 ] [0 0 1 ]
[0 1 0 ] [0 1 0 ]
[0 1 1 ] [0 1 1 ]
[0 0 1 ] [0 1 0 ]
[0 1 0 ] [0 0 1 ]
[1 0 0 ] [1 0 0 ]
[1 0 1 ] [1 0 1 ]
[1 1 0 ] [1 1 0 ]
[1 1 1 ] [1 1 1 ]
[1 0 1 ] [1 1 0 ]
[1 1 0 ] [1 0 1 ]
[0 0 1 ] [1 0 0 ]
[0 1 1 ] [1 1 0 ]
[0 1 0 ] [1 0 0 ]
[0 1 1 ] [1 0 1 ]
[1 0 0 ] [0 0 1 ]
[1 1 0 ] [0 1 1 ]
[1 0 0 ] [0 1 0 ]
[1 0 1 ] [0 1 1 ]
-----------------
Pair with same number of active bits
*/
return 0;
}``````

Output

``````  0  0  0  0  0  0
0  0  1  0  0  1
0  1  0  0  1  0
0  1  1  0  1  1
0  0  1  0  1  0
0  1  0  0  0  1
1  0  0  1  0  0
1  0  1  1  0  1
1  1  0  1  1  0
1  1  1  1  1  1
1  0  1  1  1  0
1  1  0  1  0  1
0  0  1  1  0  0
0  1  1  1  1  0
0  1  0  1  0  0
0  1  1  1  0  1
1  0  0  0  0  1
1  1  0  0  1  1
1  0  0  0  1  0
1  0  1  0  1  1``````
``````// Include namespace system
using System;
// Csharp program for
// Print all combinations of n natural number
// whose pair element difference is one
public class Combination
{
// Display calculated result
public void printResult(int[] result, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write("  " + result[i]);
}
Console.Write("\n");
}
public void subSequences(int[] result,
int start,
int index,
int difference,
int n)
{
if (index == n / 2)
{
if (difference == 0)
{
this.printResult(result, n);
}
}
else if (index >= n ||
difference > n / 2 ||
difference < -(n / 2))
{
return;
}
else
{
// Set 0 in first and second part same index position
result[index] = 0;
result[(n / 2) + index] = 0;
this.subSequences(result,
start + 1,
index + 1,
difference, n);
// Set 1 in first and second part index position
result[index] = 1;
result[(n / 2) + index] = 1;
this.subSequences(result,
start + 1,
index + 1,
difference, n);
// Set 0 in first part and 1 in second part index position
result[index] = 0;
result[(n / 2) + index] = 1;
this.subSequences(result,
start + 1,
index + 1,
difference + 1, n);
// Set 1 in first part and 0 in second part index position
result[index] = 1;
result[(n / 2) + index] = 0;
this.subSequences(result,
start + 1,
index + 1,
difference - 1, n);
}
}
public void equalDifferentParts(int n)
{
if (n <= 0 || n % 2 != 0)
{
return;
}
// Auxiliary array which is collect result
int[] result = new int[n];
this.subSequences(result, 0, 0, 0, n);
}
public static void Main(String[] args)
{
// Length of binary sequences
int n = 6;
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]
[0 0 1 ] [0 0 1 ]
[0 1 0 ] [0 1 0 ]
[0 1 1 ] [0 1 1 ]
[0 0 1 ] [0 1 0 ]
[0 1 0 ] [0 0 1 ]
[1 0 0 ] [1 0 0 ]
[1 0 1 ] [1 0 1 ]
[1 1 0 ] [1 1 0 ]
[1 1 1 ] [1 1 1 ]
[1 0 1 ] [1 1 0 ]
[1 1 0 ] [1 0 1 ]
[0 0 1 ] [1 0 0 ]
[0 1 1 ] [1 1 0 ]
[0 1 0 ] [1 0 0 ]
[0 1 1 ] [1 0 1 ]
[1 0 0 ] [0 0 1 ]
[1 1 0 ] [0 1 1 ]
[1 0 0 ] [0 1 0 ]
[1 0 1 ] [0 1 1 ]
-----------------
Pair with same number of active bits
*/
}
}``````

Output

``````  0  0  0  0  0  0
0  0  1  0  0  1
0  1  0  0  1  0
0  1  1  0  1  1
0  0  1  0  1  0
0  1  0  0  0  1
1  0  0  1  0  0
1  0  1  1  0  1
1  1  0  1  1  0
1  1  1  1  1  1
1  0  1  1  1  0
1  1  0  1  0  1
0  0  1  1  0  0
0  1  1  1  1  0
0  1  0  1  0  0
0  1  1  1  0  1
1  0  0  0  0  1
1  1  0  0  1  1
1  0  0  0  1  0
1  0  1  0  1  1``````
``````package main
import "fmt"
// Go program for
// Print all combinations of n natural number
// whose pair element difference is one

type Combination struct {}
func getCombination() * Combination {
var me *Combination = &Combination {}
return me
}
// Display calculated result
func(this Combination) printResult(result[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print("  ", result[i])
}
fmt.Print("\n")
}
func(this Combination) subSequences(result[] int,
start int,
index int,
difference int,
n int) {
if index == n / 2 {
if difference == 0 {
this.printResult(result, n)
}
} else if index >= n ||
difference > n / 2 ||
difference < -(n / 2) {
return
} else {
// Set 0 in first and second part same index position
result[index] = 0
result[(n / 2) + index] = 0
this.subSequences(result,
start + 1,
index + 1,
difference, n)
// Set 1 in first and second part index position
result[index] = 1
result[(n / 2) + index] = 1
this.subSequences(result,
start + 1,
index + 1,
difference, n)
// Set 0 in first part and 1 in second part index position
result[index] = 0
result[(n / 2) + index] = 1
this.subSequences(result,
start + 1,
index + 1,
difference + 1,
n)
// Set 1 in first part and 0 in second part index position
result[index] = 1
result[(n / 2) + index] = 0
this.subSequences(result,
start + 1,
index + 1,
difference - 1,
n)
}
}
func(this Combination) equalDifferentParts(n int) {
if n <= 0 || n % 2 != 0 {
return
}
// Auxiliary array which is collect result
var result = make([] int, n)
this.subSequences(result, 0, 0, 0, n)
}
func main() {
var task * Combination = getCombination()
// Length of binary sequences
var n int = 6
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]
[0 0 1 ] [0 0 1 ]
[0 1 0 ] [0 1 0 ]
[0 1 1 ] [0 1 1 ]
[0 0 1 ] [0 1 0 ]
[0 1 0 ] [0 0 1 ]
[1 0 0 ] [1 0 0 ]
[1 0 1 ] [1 0 1 ]
[1 1 0 ] [1 1 0 ]
[1 1 1 ] [1 1 1 ]
[1 0 1 ] [1 1 0 ]
[1 1 0 ] [1 0 1 ]
[0 0 1 ] [1 0 0 ]
[0 1 1 ] [1 1 0 ]
[0 1 0 ] [1 0 0 ]
[0 1 1 ] [1 0 1 ]
[1 0 0 ] [0 0 1 ]
[1 1 0 ] [0 1 1 ]
[1 0 0 ] [0 1 0 ]
[1 0 1 ] [0 1 1 ]
-----------------
Pair with same number of active bits
*/
}``````

Output

``````  0  0  0  0  0  0
0  0  1  0  0  1
0  1  0  0  1  0
0  1  1  0  1  1
0  0  1  0  1  0
0  1  0  0  0  1
1  0  0  1  0  0
1  0  1  1  0  1
1  1  0  1  1  0
1  1  1  1  1  1
1  0  1  1  1  0
1  1  0  1  0  1
0  0  1  1  0  0
0  1  1  1  1  0
0  1  0  1  0  0
0  1  1  1  0  1
1  0  0  0  0  1
1  1  0  0  1  1
1  0  0  0  1  0
1  0  1  0  1  1``````
``````<?php
// Php program for
// Print all combinations of n natural number
// whose pair element difference is one
class Combination
{
// Display calculated result
public	function printResult(\$result, \$n)
{
for (\$i = 0; \$i < \$n; \$i++)
{
echo("  ".\$result[\$i]);
}
echo("\n");
}
public	function subSequences(\$result,
\$start,
\$index,
\$difference,
\$n)
{
if (\$index == (int)(\$n / 2))
{
if (\$difference == 0)
{
\$this->printResult(\$result, \$n);
}
}
else if (\$index >= \$n ||
\$difference > (int)(\$n / 2) ||
\$difference < -((int)(\$n / 2)))
{
return;
}
else
{
// Set 0 in first and second part same index position
\$result[\$index] = 0;
\$result[((int)(\$n / 2)) + \$index] = 0;
\$this->subSequences(\$result,
\$start + 1,
\$index + 1,
\$difference,
\$n);
// Set 1 in first and second part index position
\$result[\$index] = 1;
\$result[((int)(\$n / 2)) + \$index] = 1;
\$this->subSequences(\$result,
\$start + 1,
\$index + 1,
\$difference,
\$n);
// Set 0 in first part and 1 in second part index position
\$result[\$index] = 0;
\$result[((int)(\$n / 2)) + \$index] = 1;
\$this->subSequences(\$result,
\$start + 1,
\$index + 1,
\$difference + 1,
\$n);
// Set 1 in first part and 0 in second part index position
\$result[\$index] = 1;
\$result[((int)(\$n / 2)) + \$index] = 0;
\$this->subSequences(\$result,
\$start + 1,
\$index + 1,
\$difference - 1,
\$n);
}
}
public	function equalDifferentParts(\$n)
{
if (\$n <= 0 || \$n % 2 != 0)
{
return;
}
// Auxiliary array which is collect result
\$result = array_fill(0, \$n, 0);
\$this->subSequences(\$result, 0, 0, 0, \$n);
}
}

function main()
{
// Length of binary sequences
\$n = 6;
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]
[0 0 1 ] [0 0 1 ]
[0 1 0 ] [0 1 0 ]
[0 1 1 ] [0 1 1 ]
[0 0 1 ] [0 1 0 ]
[0 1 0 ] [0 0 1 ]
[1 0 0 ] [1 0 0 ]
[1 0 1 ] [1 0 1 ]
[1 1 0 ] [1 1 0 ]
[1 1 1 ] [1 1 1 ]
[1 0 1 ] [1 1 0 ]
[1 1 0 ] [1 0 1 ]
[0 0 1 ] [1 0 0 ]
[0 1 1 ] [1 1 0 ]
[0 1 0 ] [1 0 0 ]
[0 1 1 ] [1 0 1 ]
[1 0 0 ] [0 0 1 ]
[1 1 0 ] [0 1 1 ]
[1 0 0 ] [0 1 0 ]
[1 0 1 ] [0 1 1 ]
-----------------
Pair with same number of active bits
*/
}
main();``````

Output

``````  0  0  0  0  0  0
0  0  1  0  0  1
0  1  0  0  1  0
0  1  1  0  1  1
0  0  1  0  1  0
0  1  0  0  0  1
1  0  0  1  0  0
1  0  1  1  0  1
1  1  0  1  1  0
1  1  1  1  1  1
1  0  1  1  1  0
1  1  0  1  0  1
0  0  1  1  0  0
0  1  1  1  1  0
0  1  0  1  0  0
0  1  1  1  0  1
1  0  0  0  0  1
1  1  0  0  1  1
1  0  0  0  1  0
1  0  1  0  1  1``````
``````// Node JS program for
// Print all combinations of n natural number
// whose pair element difference is one
class Combination
{
// Display calculated result
printResult(result, n)
{
for (var i = 0; i < n; i++)
{
process.stdout.write("  " + result[i]);
}
process.stdout.write("\n");
}
subSequences(result, start, index, difference, n)
{
if (index == parseInt(n / 2))
{
if (difference == 0)
{
this.printResult(result, n);
}
}
else if (index >= n ||
difference > parseInt(n / 2) ||
difference < -(parseInt(n / 2)))
{
return;
}
else
{
// Set 0 in first and second part same index position
result[index] = 0;
result[(parseInt(n / 2)) + index] = 0;
this.subSequences(result,
start + 1,
index + 1,
difference, n);
// Set 1 in first and second part index position
result[index] = 1;
result[(parseInt(n / 2)) + index] = 1;
this.subSequences(result,
start + 1,
index + 1,
difference,
n);
// Set 0 in first part and 1 in second part index position
result[index] = 0;
result[(parseInt(n / 2)) + index] = 1;
this.subSequences(result,
start + 1,
index + 1,
difference + 1,
n);
// Set 1 in first part and 0 in second part index position
result[index] = 1;
result[(parseInt(n / 2)) + index] = 0;
this.subSequences(result,
start + 1,
index + 1,
difference - 1,
n);
}
}
equalDifferentParts(n)
{
if (n <= 0 || n % 2 != 0)
{
return;
}
// Auxiliary array which is collect result
var result = Array(n).fill(0);
this.subSequences(result, 0, 0, 0, n);
}
}

function main()
{
// Length of binary sequences
var n = 6;
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]
[0 0 1 ] [0 0 1 ]
[0 1 0 ] [0 1 0 ]
[0 1 1 ] [0 1 1 ]
[0 0 1 ] [0 1 0 ]
[0 1 0 ] [0 0 1 ]
[1 0 0 ] [1 0 0 ]
[1 0 1 ] [1 0 1 ]
[1 1 0 ] [1 1 0 ]
[1 1 1 ] [1 1 1 ]
[1 0 1 ] [1 1 0 ]
[1 1 0 ] [1 0 1 ]
[0 0 1 ] [1 0 0 ]
[0 1 1 ] [1 1 0 ]
[0 1 0 ] [1 0 0 ]
[0 1 1 ] [1 0 1 ]
[1 0 0 ] [0 0 1 ]
[1 1 0 ] [0 1 1 ]
[1 0 0 ] [0 1 0 ]
[1 0 1 ] [0 1 1 ]
-----------------
Pair with same number of active bits
*/
}
main();``````

Output

``````  0  0  0  0  0  0
0  0  1  0  0  1
0  1  0  0  1  0
0  1  1  0  1  1
0  0  1  0  1  0
0  1  0  0  0  1
1  0  0  1  0  0
1  0  1  1  0  1
1  1  0  1  1  0
1  1  1  1  1  1
1  0  1  1  1  0
1  1  0  1  0  1
0  0  1  1  0  0
0  1  1  1  1  0
0  1  0  1  0  0
0  1  1  1  0  1
1  0  0  0  0  1
1  1  0  0  1  1
1  0  0  0  1  0
1  0  1  0  1  1``````
``````#  Python 3 program for
#  Print all combinations of n natural number
#  whose pair element difference is one
class Combination :
#  Display calculated result
def printResult(self, result, n) :
i = 0
while (i < n) :
print("  ", result[i], end = "")
i += 1

print(end = "\n")

def subSequences(self, result, start, index, difference, n) :
if (index == int(n / 2)) :
if (difference == 0) :
self.printResult(result, n)

elif (index >= n or
difference > int(n / 2) or
difference < -(int(n / 2))) :
return
else :
#  Set 0 in first and second part same index position
result[index] = 0
result[(int(n / 2)) + index] = 0
self.subSequences(result,
start + 1,
index + 1,
difference,
n)
#  Set 1 in first and second part index position
result[index] = 1
result[(int(n / 2)) + index] = 1
self.subSequences(result,
start + 1,
index + 1,
difference,
n)
#  Set 0 in first part and 1 in second part index position
result[index] = 0
result[(int(n / 2)) + index] = 1
self.subSequences(result,
start + 1,
index + 1,
difference + 1,
n)
#  Set 1 in first part and 0 in second part index position
result[index] = 1
result[(int(n / 2)) + index] = 0
self.subSequences(result,
start + 1,
index + 1,
difference - 1,
n)

def equalDifferentParts(self, n) :
if (n <= 0 or n % 2 != 0) :
return

#  Auxiliary list which is collect result
result = [0] * (n)
self.subSequences(result, 0, 0, 0, n)

def main() :
#  Length of binary sequences
n = 6
#    Length : 6
#    Equal parts size : 6 / 2 = 3
#    -----------------
#       A        B
#    -----------------
#    [0 0 0 ] [0 0 0 ]
#    [0 0 1 ] [0 0 1 ]
#    [0 1 0 ] [0 1 0 ]
#    [0 1 1 ] [0 1 1 ]
#    [0 0 1 ] [0 1 0 ]
#    [0 1 0 ] [0 0 1 ]
#    [1 0 0 ] [1 0 0 ]
#    [1 0 1 ] [1 0 1 ]
#    [1 1 0 ] [1 1 0 ]
#    [1 1 1 ] [1 1 1 ]
#    [1 0 1 ] [1 1 0 ]
#    [1 1 0 ] [1 0 1 ]
#    [0 0 1 ] [1 0 0 ]
#    [0 1 1 ] [1 1 0 ]
#    [0 1 0 ] [1 0 0 ]
#    [0 1 1 ] [1 0 1 ]
#    [1 0 0 ] [0 0 1 ]
#    [1 1 0 ] [0 1 1 ]
#    [1 0 0 ] [0 1 0 ]
#    [1 0 1 ] [0 1 1 ]
#    -----------------
#    Pair with same number of active bits

if __name__ == "__main__": main()``````

Output

``````   0   0   0   0   0   0
0   0   1   0   0   1
0   1   0   0   1   0
0   1   1   0   1   1
0   0   1   0   1   0
0   1   0   0   0   1
1   0   0   1   0   0
1   0   1   1   0   1
1   1   0   1   1   0
1   1   1   1   1   1
1   0   1   1   1   0
1   1   0   1   0   1
0   0   1   1   0   0
0   1   1   1   1   0
0   1   0   1   0   0
0   1   1   1   0   1
1   0   0   0   0   1
1   1   0   0   1   1
1   0   0   0   1   0
1   0   1   0   1   1``````
``````#  Ruby program for
#  Print all combinations of n natural number
#  whose pair element difference is one

class Combination
#  Display calculated result
def printResult(result, n)
i = 0
while (i < n)
print("  ", result[i])
i += 1
end

print("\n")
end

def subSequences(result, start, index, difference, n)
if (index == n / 2)
if (difference == 0)
self.printResult(result, n)
end

elsif (index >= n ||
difference > n / 2 ||
difference < -(n / 2))
return
else

#  Set 0 in first and second part same index position
result[index] = 0
result[(n / 2) + index] = 0
self.subSequences(result,
start + 1,
index + 1,
difference,
n)
#  Set 1 in first and second part index position
result[index] = 1
result[(n / 2) + index] = 1
self.subSequences(result,
start + 1,
index + 1,
difference,
n)
#  Set 0 in first part and 1 in second part index position
result[index] = 0
result[(n / 2) + index] = 1
self.subSequences(result,
start + 1,
index + 1,
difference + 1,
n)
#  Set 1 in first part and 0 in second part index position
result[index] = 1
result[(n / 2) + index] = 0
self.subSequences(result,
start + 1,
index + 1,
difference - 1,
n)
end

end

def equalDifferentParts(n)
if (n <= 0 || n % 2 != 0)
return
end

#  Auxiliary array which is collect result
result = Array.new(n) {0}
self.subSequences(result, 0, 0, 0, n)
end

end

def main()
#  Length of binary sequences
n = 6
#    Length : 6
#    Equal parts size : 6 / 2 = 3
#    -----------------
#       A        B
#    -----------------
#    [0 0 0 ] [0 0 0 ]
#    [0 0 1 ] [0 0 1 ]
#    [0 1 0 ] [0 1 0 ]
#    [0 1 1 ] [0 1 1 ]
#    [0 0 1 ] [0 1 0 ]
#    [0 1 0 ] [0 0 1 ]
#    [1 0 0 ] [1 0 0 ]
#    [1 0 1 ] [1 0 1 ]
#    [1 1 0 ] [1 1 0 ]
#    [1 1 1 ] [1 1 1 ]
#    [1 0 1 ] [1 1 0 ]
#    [1 1 0 ] [1 0 1 ]
#    [0 0 1 ] [1 0 0 ]
#    [0 1 1 ] [1 1 0 ]
#    [0 1 0 ] [1 0 0 ]
#    [0 1 1 ] [1 0 1 ]
#    [1 0 0 ] [0 0 1 ]
#    [1 1 0 ] [0 1 1 ]
#    [1 0 0 ] [0 1 0 ]
#    [1 0 1 ] [0 1 1 ]
#    -----------------
#    Pair with same number of active bits
end

main()``````

Output

``````  0  0  0  0  0  0
0  0  1  0  0  1
0  1  0  0  1  0
0  1  1  0  1  1
0  0  1  0  1  0
0  1  0  0  0  1
1  0  0  1  0  0
1  0  1  1  0  1
1  1  0  1  1  0
1  1  1  1  1  1
1  0  1  1  1  0
1  1  0  1  0  1
0  0  1  1  0  0
0  1  1  1  1  0
0  1  0  1  0  0
0  1  1  1  0  1
1  0  0  0  0  1
1  1  0  0  1  1
1  0  0  0  1  0
1  0  1  0  1  1
``````
``````// Scala program for
// Print all combinations of n natural number
// whose pair element difference is one
class Combination()
{
// Display calculated result
def printResult(result: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print("  " + result(i));
i += 1;
}
print("\n");
}
def subSequences(result: Array[Int],
start: Int,
index: Int,
difference: Int,
n: Int): Unit = {
if (index == n / 2)
{
if (difference == 0)
{
printResult(result, n);
}
}
else if (index >= n ||
difference > n / 2 ||
difference < -(n / 2))
{
return;
}
else
{
// Set 0 in first and second part same index position
result(index) = 0;
result((n / 2) + index) = 0;
subSequences(result,
start + 1,
index + 1,
difference,
n);
// Set 1 in first and second part index position
result(index) = 1;
result((n / 2) + index) = 1;
subSequences(result,
start + 1,
index + 1,
difference,
n);
// Set 0 in first part and 1 in second part index position
result(index) = 0;
result((n / 2) + index) = 1;
subSequences(result,
start + 1,
index + 1,
difference + 1,
n);
// Set 1 in first part and 0 in second part index position
result(index) = 1;
result((n / 2) + index) = 0;
subSequences(result,
start + 1,
index + 1,
difference - 1,
n);
}
}
def equalDifferentParts(n: Int): Unit = {
if (n <= 0 || n % 2 != 0)
{
return;
}
// Auxiliary array which is collect result
var result: Array[Int] = Array.fill[Int](n)(0);
subSequences(result, 0, 0, 0, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Combination = new Combination();
// Length of binary sequences
var n: Int = 6;
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]
[0 0 1 ] [0 0 1 ]
[0 1 0 ] [0 1 0 ]
[0 1 1 ] [0 1 1 ]
[0 0 1 ] [0 1 0 ]
[0 1 0 ] [0 0 1 ]
[1 0 0 ] [1 0 0 ]
[1 0 1 ] [1 0 1 ]
[1 1 0 ] [1 1 0 ]
[1 1 1 ] [1 1 1 ]
[1 0 1 ] [1 1 0 ]
[1 1 0 ] [1 0 1 ]
[0 0 1 ] [1 0 0 ]
[0 1 1 ] [1 1 0 ]
[0 1 0 ] [1 0 0 ]
[0 1 1 ] [1 0 1 ]
[1 0 0 ] [0 0 1 ]
[1 1 0 ] [0 1 1 ]
[1 0 0 ] [0 1 0 ]
[1 0 1 ] [0 1 1 ]
-----------------
Pair with same number of active bits
*/
}
}``````

Output

``````  0  0  0  0  0  0
0  0  1  0  0  1
0  1  0  0  1  0
0  1  1  0  1  1
0  0  1  0  1  0
0  1  0  0  0  1
1  0  0  1  0  0
1  0  1  1  0  1
1  1  0  1  1  0
1  1  1  1  1  1
1  0  1  1  1  0
1  1  0  1  0  1
0  0  1  1  0  0
0  1  1  1  1  0
0  1  0  1  0  0
0  1  1  1  0  1
1  0  0  0  0  1
1  1  0  0  1  1
1  0  0  0  1  0
1  0  1  0  1  1``````
``````// Swift 4 program for
// Print all combinations of n natural number
// whose pair element difference is one
class Combination
{
// Display calculated result
func printResult(_ result: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print("  ", result[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
func subSequences(_ result: inout[Int],
_ start: Int,
_ index: Int,
_ difference: Int,
_ n: Int)
{
if (index == n / 2)
{
if (difference == 0)
{
self.printResult(result, n);
}
}
else if (index >= n ||
difference > n / 2 ||
difference < -(n / 2))
{
return;
}
else
{
// Set 0 in first and second part same index position
result[index] = 0;
result[(n / 2) + index] = 0;
self.subSequences(&result,
start + 1,
index + 1,
difference,
n);
// Set 1 in first and second part index position
result[index] = 1;
result[(n / 2) + index] = 1;
self.subSequences(&result,
start + 1,
index + 1,
difference,
n);
// Set 0 in first part and 1 in second part index position
result[index] = 0;
result[(n / 2) + index] = 1;
self.subSequences(&result,
start + 1,
index + 1,
difference + 1,
n);
// Set 1 in first part and 0 in second part index position
result[index] = 1;
result[(n / 2) + index] = 0;
self.subSequences(&result,
start + 1,
index + 1,
difference - 1,
n);
}
}
func equalDifferentParts(_ n: Int)
{
if (n <= 0 || n % 2  != 0)
{
return;
}
// Auxiliary array which is collect result
var result: [Int] = Array(repeating: 0, count: n);
self.subSequences(&result, 0, 0, 0, n);
}
}
func main()
{
// Length of binary sequences
let n: Int = 6;
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]
[0 0 1 ] [0 0 1 ]
[0 1 0 ] [0 1 0 ]
[0 1 1 ] [0 1 1 ]
[0 0 1 ] [0 1 0 ]
[0 1 0 ] [0 0 1 ]
[1 0 0 ] [1 0 0 ]
[1 0 1 ] [1 0 1 ]
[1 1 0 ] [1 1 0 ]
[1 1 1 ] [1 1 1 ]
[1 0 1 ] [1 1 0 ]
[1 1 0 ] [1 0 1 ]
[0 0 1 ] [1 0 0 ]
[0 1 1 ] [1 1 0 ]
[0 1 0 ] [1 0 0 ]
[0 1 1 ] [1 0 1 ]
[1 0 0 ] [0 0 1 ]
[1 1 0 ] [0 1 1 ]
[1 0 0 ] [0 1 0 ]
[1 0 1 ] [0 1 1 ]
-----------------
Pair with same number of active bits
*/
}
main();``````

Output

``````   0   0   0   0   0   0
0   0   1   0   0   1
0   1   0   0   1   0
0   1   1   0   1   1
0   0   1   0   1   0
0   1   0   0   0   1
1   0   0   1   0   0
1   0   1   1   0   1
1   1   0   1   1   0
1   1   1   1   1   1
1   0   1   1   1   0
1   1   0   1   0   1
0   0   1   1   0   0
0   1   1   1   1   0
0   1   0   1   0   0
0   1   1   1   0   1
1   0   0   0   0   1
1   1   0   0   1   1
1   0   0   0   1   0
1   0   1   0   1   1``````
``````// Kotlin program for
// Print all combinations of n natural number
// whose pair element difference is one
class Combination
{
// Display calculated result
fun printResult(result: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print("  " + result[i]);
i += 1;
}
print("\n");
}
fun subSequences(result: Array < Int > ,
start: Int,
index: Int,
difference: Int,
n: Int): Unit
{
if (index == n / 2)
{
if (difference == 0)
{
this.printResult(result, n);
}
}
else if (index >= n ||
difference > n / 2 ||
difference < -(n / 2))
{
return;
}
else
{
// Set 0 in first and second part same index position
result[index] = 0;
result[(n / 2) + index] = 0;
this.subSequences(result,
start + 1,
index + 1,
difference,
n);
// Set 1 in first and second part index position
result[index] = 1;
result[(n / 2) + index] = 1;
this.subSequences(result,
start + 1,
index + 1,
difference,
n);
// Set 0 in first part and 1 in second part index position
result[index] = 0;
result[(n / 2) + index] = 1;
this.subSequences(result,
start + 1,
index + 1,
difference + 1,
n);
// Set 1 in first part and 0 in second part index position
result[index] = 1;
result[(n / 2) + index] = 0;
this.subSequences(result,
start + 1,
index + 1,
difference - 1,
n);
}
}
fun equalDifferentParts(n: Int): Unit
{
if (n <= 0 || n % 2 != 0)
{
return;
}
// Auxiliary array which is collect result
var result: Array < Int > = Array(n)
{
0
};
this.subSequences(result, 0, 0, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
// Length of binary sequences
val n: Int = 6;
/*
Length : 6
Equal parts size : 6 / 2 = 3
-----------------
A        B
-----------------
[0 0 0 ] [0 0 0 ]
[0 0 1 ] [0 0 1 ]
[0 1 0 ] [0 1 0 ]
[0 1 1 ] [0 1 1 ]
[0 0 1 ] [0 1 0 ]
[0 1 0 ] [0 0 1 ]
[1 0 0 ] [1 0 0 ]
[1 0 1 ] [1 0 1 ]
[1 1 0 ] [1 1 0 ]
[1 1 1 ] [1 1 1 ]
[1 0 1 ] [1 1 0 ]
[1 1 0 ] [1 0 1 ]
[0 0 1 ] [1 0 0 ]
[0 1 1 ] [1 1 0 ]
[0 1 0 ] [1 0 0 ]
[0 1 1 ] [1 0 1 ]
[1 0 0 ] [0 0 1 ]
[1 1 0 ] [0 1 1 ]
[1 0 0 ] [0 1 0 ]
[1 0 1 ] [0 1 1 ]
-----------------
Pair with same number of active bits
*/
}``````

Output

``````  0  0  0  0  0  0
0  0  1  0  0  1
0  1  0  0  1  0
0  1  1  0  1  1
0  0  1  0  1  0
0  1  0  0  0  1
1  0  0  1  0  0
1  0  1  1  0  1
1  1  0  1  1  0
1  1  1  1  1  1
1  0  1  1  1  0
1  1  0  1  0  1
0  0  1  1  0  0
0  1  1  1  1  0
0  1  0  1  0  0
0  1  1  1  0  1
1  0  0  0  0  1
1  1  0  0  1  1
1  0  0  0  1  0
1  0  1  0  1  1``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post