Skip to main content

Generate all 0 and 1 with 25% and 75% probability

Here given code implementation process.

// C Program
// Generate all 0 and 1 with 25% and 75% probability
#include <stdio.h>

void balancedBinary(char result[], int k, 
  					int zero, int one, int index)
{
	if (zero > k || one > k *3)
	{
		return;
	}
	if (index == k *4)
	{
		printf("%s\n", result);
		return;
	}
	result[index] = '0';
	balancedBinary(result, k, zero + 1, one, index + 1);
	result[index] = '1';
	balancedBinary(result, k, zero, one + 1, index + 1);
}
void findSolution(int n)
{
	if (n <= 0)
	{
		return;
	}
	if (n < 4)
	{
		// When only 1s
		for (int i = 0; i < n; ++i)
		{
			printf("  1");
		}
		return;
	}
	char result[n + 1];
	// set the value of last char is to terminating character
	result[n - 1] = '\0';
	balancedBinary(result, n / 4, 0, 0, 0);
}
int main()
{
	int n = 8;
	// Example
	// n = 8
	// k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
	// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
	findSolution(n);
	return 0;
}

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
// Java program for
// Generate all 0 and 1 with 25% and 75% probability
public class Combination
{
	public void balancedBinary(String result, 
                                int k, int zero, 
                                int one, int index)
	{
		if (zero > k || one > k * 3)
		{
			return;
		}
		if (index == k * 4)
		{
			System.out.println(result);
			return;
		}
		balancedBinary(result + "0", k, 
                       zero + 1, one, index + 1);
		balancedBinary(result + "1", k, 
                       zero, one + 1, index + 1);
	}
	public void findSolution(int n)
	{
		if (n <= 0)
		{
			return;
		}
		if (n < 4)
		{
			// When only 1s
			for (int i = 0; i < n; ++i)
			{
				System.out.print(" 1");
			}
			return;
		}
		balancedBinary("", n / 4, 0, 0, 0);
	}
	public static void main(String[] args)
	{
		Combination task = new Combination();
		int n = 8;
		// Example
		// n = 8
		// k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
		// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
		task.findSolution(n);
	}
}

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Generate all 0 and 1 with 25% and 75% probability
class Combination
{
	public: void balancedBinary(string result, 
                                int k, int zero, 
                                int one, int index)
	{
		if (zero > k || one > k *3)
		{
			return;
		}
		if (index == k *4)
		{
			cout << result << endl;
			return;
		}
		this->balancedBinary(result  +  "0", 
                             k, zero + 1, one, index + 1);
		this->balancedBinary(result  +  "1", 
                             k, zero, one + 1, index + 1);
	}
	void findSolution(int n)
	{
		if (n <= 0)
		{
			return;
		}
		if (n < 4)
		{
			// When only 1s
			for (int i = 0; i < n; ++i)
			{
				cout << " 1";
			}
			return;
		}
		this->balancedBinary("", n / 4, 0, 0, 0);
	}
};
int main()
{
	Combination *task = new Combination();
	int n = 8;
	// Example
	// n = 8
	// k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
	// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
	task->findSolution(n);
	return 0;
}

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
// Include namespace system
using System;
// Csharp program for
// Generate all 0 and 1 with 25% and 75% probability
public class Combination
{
	public void balancedBinary(String result, 
                                int k, int zero, int one, int index)
	{
		if (zero > k || one > k * 3)
		{
			return;
		}
		if (index == k * 4)
		{
			Console.WriteLine(result);
			return;
		}
		this.balancedBinary(result + "0", k, 
                            zero + 1, one, index + 1);
		this.balancedBinary(result + "1", k, 
                            zero, one + 1, index + 1);
	}
	public void findSolution(int n)
	{
		if (n <= 0)
		{
			return;
		}
		if (n < 4)
		{
			// When only 1s
			for (int i = 0; i < n; ++i)
			{
				Console.Write(" 1");
			}
			return;
		}
		this.balancedBinary("", n / 4, 0, 0, 0);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		int n = 8;
		// Example
		// n = 8
		// k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
		// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
		task.findSolution(n);
	}
}

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
package main
import "fmt"
// Go program for
// Generate all 0 and 1 with 25% and 75% probability
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
func(this Combination) balancedBinary(result string, 
                                      k int, zero int, 
                                      one int, index int) {
	if zero > k || one > k * 3 {
		return
	}
	if index == k * 4 {
		fmt.Println(result)
		return
	}
	this.balancedBinary(result + "0", 
		k, zero + 1, one, index + 1)
	this.balancedBinary(result + "1", 
		k, zero, one + 1, index + 1)
}
func(this Combination) findSolution(n int) {
	if n <= 0 {
		return
	}
	if n < 4 {
		// When only 1s
		for i := 0 ; i < n ; i++ {
			fmt.Print(" 1")
		}
		return
	}
	this.balancedBinary("", n / 4, 0, 0, 0)
}
func main() {
	var task * Combination = getCombination()
	var n int = 8
	// Example
	// n = 8
	// k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
	// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
	task.findSolution(n)
}

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
<?php
// Php program for
// Generate all 0 and 1 with 25% and 75% probability
class Combination
{
	public	function balancedBinary($result, $k, 
                                     $zero, $one, 
                                     $index)
	{
		if ($zero > $k || $one > $k * 3)
		{
			return;
		}
		if ($index == $k * 4)
		{
			echo($result.
				"\n");
			return;
		}
		$this->balancedBinary($result.
			"0", $k, $zero + 1, $one, $index + 1);
		$this->balancedBinary($result.
			"1", $k, $zero, $one + 1, $index + 1);
	}
	public	function findSolution($n)
	{
		if ($n <= 0)
		{
			return;
		}
		if ($n < 4)
		{
			// When only 1s
			for ($i = 0; $i < $n; ++$i)
			{
				echo(" 1");
			}
			return;
		}
		$this->balancedBinary("", (int)($n / 4), 0, 0, 0);
	}
}

function main()
{
	$task = new Combination();
	$n = 8;
	// Example
	// n = 8
	// k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
	// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
	$task->findSolution($n);
}
main();

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
// Node JS program for
// Generate all 0 and 1 with 25% and 75% probability
class Combination
{
	balancedBinary(result, k, zero, one, index)
	{
		if (zero > k || one > k * 3)
		{
			return;
		}
		if (index == k * 4)
		{
			console.log(result);
			return;
		}
		this.balancedBinary(result + "0", k, 
                            zero + 1, one, index + 1);
		this.balancedBinary(result + "1", k, 
                            zero, one + 1, index + 1);
	}
	findSolution(n)
	{
		if (n <= 0)
		{
			return;
		}
		if (n < 4)
		{
			// When only 1s
			for (var i = 0; i < n; ++i)
			{
				process.stdout.write(" 1");
			}
			return;
		}
		this.balancedBinary("", parseInt(n / 4), 0, 0, 0);
	}
}

function main()
{
	var task = new Combination();
	var n = 8;
	// Example
	// n = 8
	// k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
	// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
	task.findSolution(n);
}
main();

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
#  Python 3 program for
#  Generate all 0 and 1 with 25% and 75% probability
class Combination :
	def balancedBinary(self, result, k, zero, one, index) :
		if (zero > k or one > k * 3) :
			return
		
		if (index == k * 4) :
			print(result)
			return
		
		self.balancedBinary(result + "0", k, 
                            zero + 1, one, index + 1)
		self.balancedBinary(result + "1", k, 
                            zero, one + 1, index + 1)
	
	def findSolution(self, n) :
		if (n <= 0) :
			return
		
		if (n < 4) :
			i = 0
			#  When only 1s
			while (i < n) :
				print(" 1", end = "")
				i += 1
			
			return
		
		self.balancedBinary("", int(n / 4), 0, 0, 0)
	

def main() :
	task = Combination()
	n = 8
	#  Example
	#  n = 8
	#  k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
	#  k *3        [2*3 = 6 (six 1s)] (75 % 1s)
	task.findSolution(n)

if __name__ == "__main__": main()

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
#  Ruby program for
#  Generate all 0 and 1 with 25% and 75% probability
class Combination 
	def balancedBinary(result, k, zero, one, index) 
		if (zero > k || one > k * 3) 
			return
		end

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

		self.balancedBinary(result + "0", k, 
                            zero + 1, one, index + 1)
		self.balancedBinary(result + "1", k, 
                            zero, one + 1, index + 1)
	end

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

		if (n < 4) 
			i = 0
			#  When only 1s
			while (i < n) 
				print(" 1")
				i += 1
			end

			return
		end

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

end

def main() 
	task = Combination.new()
	n = 8
	#  Example
	#  n = 8
	#  k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
	#  k *3        [2*3 = 6 (six 1s)] (75 % 1s)
	task.findSolution(n)
end

main()

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
// Scala program for
// Generate all 0 and 1 with 25% and 75% probability
class Combination()
{
	def balancedBinary(result: String, k: Int, 
                       zero: Int, one: Int, index: Int): Unit = {
		if (zero > k || one > k * 3)
		{
			return;
		}
		if (index == k * 4)
		{
			println(result);
			return;
		}
		balancedBinary(result + "0", k, 
                       zero + 1, one, index + 1);
		balancedBinary(result + "1", k, 
                       zero, one + 1, index + 1);
	}
	def findSolution(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		if (n < 4)
		{
			var i: Int = 0;
			// When only 1s
			while (i < n)
			{
				print(" 1");
				i += 1;
			}
			return;
		}
		balancedBinary("", n / 4, 0, 0, 0);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		var n: Int = 8;
		// Example
		// n = 8
		// k = n / 4   [8/4 = 2 (two 0s)] (25 % 0s)
		// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
		task.findSolution(n);
	}
}

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
// Swift 4 program for
// Generate all 0 and 1 with 25% and 75% probability
class Combination
{
	func balancedBinary(_ result: String, _ k: Int, 
                         _ zero: Int, _ one: Int, _ index: Int)
	{
		if (zero > k || one > k * 3)
		{
			return;
		}
		if (index == k * 4)
		{
			print(result);
			return;
		}
		self.balancedBinary(result + "0", k, 
                            zero + 1, one, index + 1);
		self.balancedBinary(result + "1", k, 
                            zero, one + 1, index + 1);
	}
	func findSolution(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		if (n < 4)
		{
			var i: Int = 0;
			// When only 1s
			while (i < n)
			{
				print(" 1", terminator: "");
				i += 1;
			}
			return;
		}
		self.balancedBinary("", n / 4, 0, 0, 0);
	}
}
func main()
{
	let task: Combination = Combination();
	let n: Int = 8;
	// Example
	// n = 8
	// k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
	// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
	task.findSolution(n);
}
main();

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100
// Kotlin program for
// Generate all 0 and 1 with 25% and 75% probability
class Combination
{
	fun balancedBinary(result: String, k: Int, zero: Int, 
                        one: Int, index: Int): Unit
	{
		if (zero > k || one > k * 3)
		{
			return;
		}
		if (index == k * 4)
		{
			println(result);
			return;
		}
		this.balancedBinary(result + "0", k, zero + 1, one, index + 1);
		this.balancedBinary(result + "1", k, zero, one + 1, index + 1);
	}
	fun findSolution(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		if (n < 4)
		{
			var i: Int = 0;
			// When only 1s
			while (i < n)
			{
				print(" 1");
				i += 1;
			}
			return;
		}
		this.balancedBinary("", n / 4, 0, 0, 0);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	val n: Int = 8;
	// Example
	// n = 8
	// k = n / 4    [8/4 = 2 (two 0s)] (25 % 0s)
	// k *3        [2*3 = 6 (six 1s)] (75 % 1s)
	task.findSolution(n);
}

Output

00111111
01011111
01101111
01110111
01111011
01111101
01111110
10011111
10101111
10110111
10111011
10111101
10111110
11001111
11010111
11011011
11011101
11011110
11100111
11101011
11101101
11101110
11110011
11110101
11110110
11111001
11111010
11111100




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