Generate all Binary Strings of length N with equal count of 0s and 1s
Last Updated :
29 Dec, 2021
Given an integer N, the task is to generate all the binary strings with equal 0s and 1s. If no strings are possible, print -1
Examples:
Input: N = 2
Output: “01”, “10”
Explanation: All possible binary strings of length 2 are: 01, 10, 11, 00. Out of these, only 2 have equal number of 0s and 1s
Input: 4
Output: “0011”, “0101”, “0110”, “1100”, “1010”, “1001”
Approach: The task can be solved by using recursion. If N is odd, then the answer is -1, else, we can use recursion to generate all the binary strings with equal 0s and 1s. Follow the below steps to solve the problem:
- Variable ones keep track of the number of 1's and variable zeros keeps a track of the number of 0's in the string.
- Both ones and zeros should have frequency N/2.
- Base condition: The string s stores the output string. So, when the length of s reaches N we stop recursive calls and print the output string s.
- If the frequency of 1's is less than N/2 then add 1 to the string and increment ones.
- If the frequency of 0's is less than N/2 then add 0 to the string and increment zeros.
Below is the implementation of the above code:
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Recursive function that prints
// all strings of N length with equal 1's and 0's
void binaryNum(int n, string s, int ones,
int zeros)
{
// String s contains the output to be printed
// ones stores the frequency of 1's
// zeros stores the frequency of 0's
// Base Condition: When the length of string s
// becomes N
if (s.length() == n)
{
cout << (s) << endl;
return;
}
// If frequency of 1's is less than N/2 then
// add 1 to the string and increment ones
if (ones < n / 2)
binaryNum(n, s + "1", ones + 1, zeros);
// If frequency of 0's is less than N/2 then
// add 0 to the string and increment zeros
if (zeros < n / 2)
binaryNum(n, s + "0", ones, zeros + 1);
}
// Driver Code
int main()
{
string s = "";
binaryNum(4, s, 0, 0);
return 0;
}
// This code is contributed by Potta Lokesh
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Recursive function that prints
// all strings of N length with equal 1's and 0's
static void binaryNum(int n, String s, int ones,
int zeros)
{
// String s contains the output to be printed
// ones stores the frequency of 1's
// zeros stores the frequency of 0's
// Base Condition: When the length of string s
// becomes N
if (s.length() == n) {
System.out.println(s);
return;
}
// If frequency of 1's is less than N/2 then
// add 1 to the string and increment ones
if (ones < n / 2)
binaryNum(n, s + "1", ones + 1, zeros);
// If frequency of 0's is less than N/2 then
// add 0 to the string and increment zeros
if (zeros < n / 2)
binaryNum(n, s + "0", ones, zeros + 1);
}
// Driver Code
public static void main(String[] args)
{
String s = "";
binaryNum(4, s, 0, 0);
}
}
Python3
# python code for the above approach
# Recursive function that prints
# all strings of N length with equal 1's and 0's
def binaryNum(n, s, ones, zeros):
# String s contains the output to be printed
# ones stores the frequency of 1's
# zeros stores the frequency of 0's
# Base Condition: When the length of string s
# becomes N
if (len(s) == n):
print(s)
return
# If frequency of 1's is less than N/2 then
# add 1 to the string and increment ones
if (ones < n / 2):
binaryNum(n, s + "1", ones + 1, zeros)
# If frequency of 0's is less than N/2 then
# add 0 to the string and increment zeros
if (zeros < n / 2):
binaryNum(n, s + "0", ones, zeros + 1)
# Driver Code
if __name__ == "__main__":
s = ""
binaryNum(4, s, 0, 0)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG {
// Recursive function that prints
// all strings of N length with equal 1's and 0's
static void binaryNum(int n, string s, int ones,
int zeros)
{
// String s contains the output to be printed
// ones stores the frequency of 1's
// zeros stores the frequency of 0's
// Base Condition: When the length of string s
// becomes N
if (s.Length == n) {
Console.WriteLine(s);
return;
}
// If frequency of 1's is less than N/2 then
// add 1 to the string and increment ones
if (ones < n / 2)
binaryNum(n, s + "1", ones + 1, zeros);
// If frequency of 0's is less than N/2 then
// add 0 to the string and increment zeros
if (zeros < n / 2)
binaryNum(n, s + "0", ones, zeros + 1);
}
// Driver Code
public static void Main(string[] args)
{
string s = "";
binaryNum(4, s, 0, 0);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// javascript program for the above approach
// Recursive function that prints
// all strings of N length with equal 1's and 0's
function binaryNum(n, s, ones, zeros)
{
// String s contains the output to be printed
// ones stores the frequency of 1's
// zeros stores the frequency of 0's
// Base Condition: When the length of string s
// becomes N
if (s.length == n) {
document.write(s+"<br>");
return;
}
// If frequency of 1's is less than N/2 then
// add 1 to the string and increment ones
if (ones < n / 2)
binaryNum(n, s + "1", ones + 1, zeros);
// If frequency of 0's is less than N/2 then
// add 0 to the string and increment zeros
if (zeros < n / 2)
binaryNum(n, s + "0", ones, zeros + 1);
}
// Driver Code
var s = "";
binaryNum(4, s, 0, 0);
// This code is contributed by 29AjayKumar
</script>
Output1100
1010
1001
0110
0101
0011
Time Complexity: O(2N)
Auxiliary Space: O(1)
Similar Reads
Count of binary string of length N with X 0s and Y 1s Given positive integers N, X and Y. The task is to find the count of unique binary strings of length N having X 0s and Y 1s. Examples: Input: N=5, X=3, Y=2Output: 10Explanation: There are 10 binary strings of length 5 with 3 0s and 2 1s, such as: 00011, 00101, 01001, 10001, 00110, 01010, 10010, 0110
4 min read
Longest balanced binary substring with equal count of 1s and 0s Given a binary string str[] of size N. The task is to find the longest balanced substring. A substring is balanced if it contains an equal number of 0 and 1. Examples: Input: str = "110101010"Output: 10101010Explanation: The formed substring contain equal count of 1 and 0 i.e, count of 1 and 0 is sa
8 min read
Check if all substrings of length K of a Binary String has equal count of 0s and 1s Given a binary string S of length N and an even integer K, the task is to check if all substrings of length K contains an equal number of 0s and 1s. If found to be true, print âYesâ. Otherwise, print âNoâ. Examples: Input: S = "101010", K = 2Output: YesExplanation:Since all the substrings of length
6 min read
Count of binary strings of length N having equal count of 0's and 1's Given an integer N, the task is to find the number of binary strings possible of length N having same frequency of 0s and 1s. If such string is possible of length N, print -1. Note: Since the count can be very large, return the answer modulo 109+7.Examples: Input: N = 2 Output: 2 Explanation: All po
6 min read
Count Substrings with equal number of 0s, 1s and 2s Given a string that consists of only 0s, 1s and 2s, count the number of substrings that have an equal number of 0s, 1s, and 2s.Examples: Input: str = â0102010âOutput: 2Explanation: Substring str[2, 4] = â102â and substring str[4, 6] = â201â has equal number of 0, 1 and 2Input: str = "102100211"Outpu
9 min read
Generate Binary String with equal number of 01 and 10 Subsequence Given an integer N (N > 2), the task is to generate a binary string of size N that consists of equal numbers of "10" & "01" subsequences and also the string should contain at least one '0' and one '1' Note: If multiple such strings exist, print any. Examples: Input: 4Output: 0110Explanation :
7 min read
Count of substrings with equal ratios of 0s and 1s till ith index in given Binary String Given a binary string S, the task is to print the maximum number of substrings with equal ratios of 0s and 1s till the ith index from the start. Examples: Input: S = "110001"Output: {1, 2, 1, 1, 1, 2}Explanation: The given string can be partitioned into the following equal substrings: Valid substrin
9 min read
Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s Given three integers N, P, and Q, the task is to count all possible distinct binary strings of length N such that each binary string does not contain P times consecutive 0âs and Q times consecutive 1's. Examples: Input: N = 5, P = 2, Q = 3Output: 7Explanation: Binary strings that satisfy the given c
15+ min read
Count of all possible N length balanced Binary Strings Given a number N, the task is to find the total number of balanced binary strings possible of length N. A binary string is said to be balanced if: The number of 0s and 1s are equal in each binary stringThe count of 0s in any prefix of binary strings is always greater than or equal to the count of 1s
6 min read