Skip to main content

Print all combinations of balanced binary

A balanced binary string is a binary string in which the number of 0s and 1s are equal. For example, "0101" and "1100" are balanced binary strings, but "01110" and "101" are not.

To print all combinations of balanced binary strings, we can use a recursive approach. Starting with an empty string, we can generate all possible combinations of balanced binary strings by adding either a 0 or a 1 to the string, as long as the number of 0s and 1s in the string are equal.

Here is the algorithm:

  1. Start with an empty string.

  2. For each recursion, we have two choices: add a 0 or a 1 to the string.

  3. If the number of 0s and 1s in the string are equal, we can print the string as a balanced binary string.

  4. If the length of the string is less than the desired length, we can continue with the recursion by calling the function recursively with the new string.

  5. When the length of the string is equal to the desired length, the function returns and the recursion ends.

Code Example

// C Program
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
#include <stdio.h>

void balancedBinary(char result[], int n, int zero, int one, int index)
{
	if (zero > n || one > n)
	{
		return;
	}
	if (index == n *2)
	{
		printf("%s\n", result);
		return;
	}
	// Set 0's 
	result[index] = '0';
	balancedBinary(result, n, zero + 1, one, index + 1);
	// Set 1's 
	result[index] = '1';
	balancedBinary(result, n, zero, one + 1, index + 1);
}
void findSolution(int n)
{
	if (n <= 0)
	{
		return;
	}
	int k = n *2 + 1;
	char result[k];
	// set the value of last char is to terminating character
	result[k - 1] = '\0';
	balancedBinary(result, n, 0, 0, 0);
}
int main()
{
	int n = 3;
	findSolution(n);
	return 0;
}

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
// Java program for
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
public class Combination
{
    public void balancedBinary(String result, 
                                int n, 
                                int zero, 
                                int one, 
                                int index)
    {
        if (zero > n || one > n)
        {
            return;
        }
        if (index == n * 2)
        {
            System.out.println( result );
            return;
        }
        // Set 0's 
     
        balancedBinary(result+"0", n, zero + 1, one, index + 1);
        // Set 1's 
        balancedBinary(result+"1", n, zero, one + 1, index + 1);
    }
    public void findSolution(int n)
    {
        if (n <= 0)
        {
            return;
        }
        balancedBinary("", n, 0, 0, 0);
    }
    public static void main(String[] args)
    {
        Combination task = new Combination();

        int n = 3;
        
        task.findSolution(n);
    }
}

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
class Combination
{
	public: void balancedBinary(string result, 
                                int n, 
                                int zero, 
                                int one, 
                                int index)
	{
		if (zero > n || one > n)
		{
			return;
		}
		if (index == n *2)
		{
			cout << result << endl;
			return;
		}
		// Set 0's 
		this->balancedBinary(result  +  "0", n, 
                             zero + 1, one, index + 1);
		// Set 1's 
		this->balancedBinary(result  +  "1", n, 
                             zero, one + 1, index + 1);
	}
	void findSolution(int n)
	{
		if (n <= 0)
		{
			return;
		}
		this->balancedBinary("", n, 0, 0, 0);
	}
};
int main()
{
	Combination *task = new Combination();
	int n = 3;
	task->findSolution(n);
	return 0;
}

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
// Include namespace system
using System;
// Csharp program for
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
public class Combination
{
	public void balancedBinary(String result, 
                                int n, 
                                int zero, 
                                int one, 
                                int index)
	{
		if (zero > n || one > n)
		{
			return;
		}
		if (index == n * 2)
		{
			Console.WriteLine(result);
			return;
		}
		// Set 0's 
		this.balancedBinary(result + "0", n, zero + 1, one, index + 1);
		// Set 1's 
		this.balancedBinary(result + "1", n, zero, one + 1, index + 1);
	}
	public void findSolution(int n)
	{
		if (n <= 0)
		{
			return;
		}
		this.balancedBinary("", n, 0, 0, 0);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		int n = 3;
		task.findSolution(n);
	}
}

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
package main
import "fmt"
// Go program for
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
func(this Combination) balancedBinary(result string, 
	n int, zero int, one int, index int) {
	if zero > n || one > n {
		return
	}
	if index == n * 2 {
		fmt.Println(result)
		return
	}
	// Set 0's 
	this.balancedBinary(result + "0", n, 
		zero + 1, one, index + 1)
	// Set 1's 
	this.balancedBinary(result + "1", n, 
		zero, one + 1, index + 1)
}
func(this Combination) findSolution(n int) {
	if n <= 0 {
		return
	}
	this.balancedBinary("", n, 0, 0, 0)
}
func main() {
	var task * Combination = getCombination()
	var n int = 3
	task.findSolution(n)
}

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
<?php
// Php program for
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
class Combination
{
	public	function balancedBinary($result, 
                                     $n, 
                                     $zero,
                                     $one, 
                                     $index)
	{
		if ($zero > $n || $one > $n)
		{
			return;
		}
		if ($index == $n * 2)
		{
			echo($result."\n");
			return;
		}
		// Set 0's 
		$this->balancedBinary($result."0", $n, 
                              $zero + 1, $one, $index + 1);
		// Set 1's 
		$this->balancedBinary($result."1", $n, 
                              $zero, $one + 1, $index + 1);
	}
	public	function findSolution($n)
	{
		if ($n <= 0)
		{
			return;
		}
		$this->balancedBinary("", $n, 0, 0, 0);
	}
}

function main()
{
	$task = new Combination();
	$n = 3;
	$task->findSolution($n);
}
main();

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
// Node JS program for
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
class Combination
{
	balancedBinary(result, n, zero, one, index)
	{
		if (zero > n || one > n)
		{
			return;
		}
		if (index == n * 2)
		{
			console.log(result);
			return;
		}
		// Set 0's 
		this.balancedBinary(result + "0", n, 
                            zero + 1, one, index + 1);
		// Set 1's 
		this.balancedBinary(result + "1", n, 
                            zero, one + 1, index + 1);
	}
	findSolution(n)
	{
		if (n <= 0)
		{
			return;
		}
		this.balancedBinary("", n, 0, 0, 0);
	}
}

function main()
{
	var task = new Combination();
	var n = 3;
	task.findSolution(n);
}
main();

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
#  Python 3 program for
#  Print all combinations of balanced binary
#  50 % 0 and 50 % 1 Probability
class Combination :
	def balancedBinary(self, result, n, zero, one, index) :
		if (zero > n or one > n) :
			return
		
		if (index == n * 2) :
			print(result)
			return
		
		#  Set 0's 
		self.balancedBinary(result + "0", n, 
                            zero + 1, one, index + 1)
		#  Set 1's 
		self.balancedBinary(result + "1", n, 
                            zero, one + 1, index + 1)
	
	def findSolution(self, n) :
		if (n <= 0) :
			return
		
		self.balancedBinary("", n, 0, 0, 0)
	

def main() :
	task = Combination()
	n = 3
	task.findSolution(n)

if __name__ == "__main__": main()

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
#  Ruby program for
#  Print all combinations of balanced binary
#  50 % 0 and 50 % 1 Probability
class Combination 
	def balancedBinary(result, n, zero, one, index) 
		if (zero > n || one > n) 
			return
		end

		if (index == n * 2) 
			print(result, "\n")
			return
		end

		#  Set 0's 
		self.balancedBinary(result + "0", n, 
                            zero + 1, one, index + 1)
		#  Set 1's 
		self.balancedBinary(result + "1", n, 
                            zero, one + 1, index + 1)
	end

	def findSolution(n) 
		if (n <= 0) 
			return
		end

		self.balancedBinary("", n, 0, 0, 0)
	end

end

def main() 
	task = Combination.new()
	n = 3
	task.findSolution(n)
end

main()

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
// Scala program for
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
class Combination()
{
	def balancedBinary(result: String, n: Int, 
                       zero: Int, one: Int, 
                       index: Int): Unit = {
		if (zero > n || one > n)
		{
			return;
		}
		if (index == n * 2)
		{
			println(result);
			return;
		}
		// Set 0's 
		balancedBinary(result + "0", n, zero + 1, one, index + 1);
		// Set 1's 
		balancedBinary(result + "1", n, zero, one + 1, index + 1);
	}
	def findSolution(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		balancedBinary("", n, 0, 0, 0);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		var n: Int = 3;
		task.findSolution(n);
	}
}

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
// Swift 4 program for
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
class Combination
{
	func balancedBinary(_ result: String, 
                         _ n: Int, 
                         _ zero: Int, 
                         _ one: Int,
                         _ index: Int)
	{
		if (zero > n || one > n)
		{
			return;
		}
		if (index == n * 2)
		{
			print(result);
			return;
		}
		// Set 0's 
		self.balancedBinary(result + "0", n, 
                            zero + 1, one, index + 1);
		// Set 1's 
		self.balancedBinary(result + "1", n, 
                            zero, one + 1, index + 1);
	}
	func findSolution(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		self.balancedBinary("", n, 0, 0, 0);
	}
}
func main()
{
	let task: Combination = Combination();
	let n: Int = 3;
	task.findSolution(n);
}
main();

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
// Kotlin program for
// Print all combinations of balanced binary
// 50 % 0 and 50 % 1 Probability
class Combination
{
	fun balancedBinary(result: String, n: Int, 
                        zero: Int, one: Int, index: Int): Unit
	{
		if (zero > n || one > n)
		{
			return;
		}
		if (index == n * 2)
		{
			println(result);
			return;
		}
		// Set 0's 
		this.balancedBinary(result + "0", n, 
                            zero + 1, one, index + 1);
		// Set 1's 
		this.balancedBinary(result + "1", n, 
                            zero, one + 1, index + 1);
	}
	fun findSolution(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		this.balancedBinary("", n, 0, 0, 0);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	val n: Int = 3;
	task.findSolution(n);
}

Output

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000




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.

New Comment