Given a positive integer number N. The task is to generate all the binary strings of N bits. These binary strings should be in ascending order.
Examples:
Input: 2 Output: 0 0 0 1 1 0 1 1 Input: 3 Output: 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
Approach: The idea is to try every permutation. For every position, there are 2 options, either ‘0’ or ‘1’. Backtracking is used in this approach to try every possibility/permutation.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach: #include <bits/stdc++.h> using namespace std; // Function to print the output void printTheArray(int arr[], int n) { for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } // Function to generate all binary strings void generateAllBinaryStrings(int n, int arr[], int i) { if (i == n) { printTheArray(arr, n); return; } // First assign "0" at ith position // and try for all other permutations // for remaining positions arr[i] = 0; generateAllBinaryStrings(n, arr, i + 1); // And then assign "1" at ith position // and try for all other permutations // for remaining positions arr[i] = 1; generateAllBinaryStrings(n, arr, i + 1); } // Driver Code int main() { int n = 4; int arr[n]; // Print all binary strings generateAllBinaryStrings(n, arr, 0); return 0; } |
Java
// Java implementation of the above approach: import java.util.*; class GFG { // Function to print the output static void printTheArray(int arr[], int n) { for (int i = 0; i < n; i++) { System.out.print(arr[i]+" "); } System.out.println(); } // Function to generate all binary strings static void generateAllBinaryStrings(int n, int arr[], int i) { if (i == n) { printTheArray(arr, n); return; } // First assign "0" at ith position // and try for all other permutations // for remaining positions arr[i] = 0; generateAllBinaryStrings(n, arr, i + 1); // And then assign "1" at ith position // and try for all other permutations // for remaining positions arr[i] = 1; generateAllBinaryStrings(n, arr, i + 1); } // Driver Code public static void main(String args[]) { int n = 4; int[] arr = new int[n]; // Print all binary strings generateAllBinaryStrings(n, arr, 0); } } // This code is contributed by // Surendra_Gangwar |
Python3
# Python3 implementation of the # above approach # Function to print the output def printTheArray(arr, n): for i in range(0, n): print(arr[i], end = " ") print() # Function to generate all binary strings def generateAllBinaryStrings(n, arr, i): if i == n: printTheArray(arr, n) return # First assign "0" at ith position # and try for all other permutations # for remaining positions arr[i] = 0 generateAllBinaryStrings(n, arr, i + 1) # And then assign "1" at ith position # and try for all other permutations # for remaining positions arr[i] = 1 generateAllBinaryStrings(n, arr, i + 1) # Driver Code if __name__ == "__main__": n = 4 arr = [None] * n # Print all binary strings generateAllBinaryStrings(n, arr, 0) # This code is contributed # by Rituraj Jain |
C#
// C# implementation of the above approach: using System; class GFG { // Function to print the output static void printTheArray(int []arr, int n) { for (int i = 0; i < n; i++) { Console.Write(arr[i]+" "); } Console.WriteLine(); } // Function to generate all binary strings static void generateAllBinaryStrings(int n, int []arr, int i) { if (i == n) { printTheArray(arr, n); return; } // First assign "0" at ith position // and try for all other permutations // for remaining positions arr[i] = 0; generateAllBinaryStrings(n, arr, i + 1); // And then assign "1" at ith position // and try for all other permutations // for remaining positions arr[i] = 1; generateAllBinaryStrings(n, arr, i + 1); } // Driver Code public static void Main(String []args) { int n = 4; int[] arr = new int[n]; // Print all binary strings generateAllBinaryStrings(n, arr, 0); } } // This code has been contributed by 29AjayKumar |
PHP
<?php // PHP implementation of the above approach // Function to print the output function printTheArray($arr, $n) { for ($i = 0; $i < $n; $i++) { echo $arr[$i], " "; } echo "\n"; } // Function to generate all binary strings function generateAllBinaryStrings($n, $arr, $i) { if ($i == $n) { printTheArray($arr, $n); return; } // First assign "0" at ith position // and try for all other permutations // for remaining positions $arr[$i] = 0; generateAllBinaryStrings($n, $arr, $i + 1); // And then assign "1" at ith position // and try for all other permutations // for remaining positions $arr[$i] = 1; generateAllBinaryStrings($n, $arr, $i + 1); } // Driver Code $n = 4; $arr = array_fill(0, $n, 0); // Print all binary strings generateAllBinaryStrings($n, $arr, 0); // This code is contributed by Ryuga ?> |
0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1
Related Article: Generate all the binary number from 0 to n
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Generate all binary strings without consecutive 1's
- Generate all binary strings from given pattern
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Generate Binary Strings of length N using Branch and Bound
- Number of Binary Strings of length N with K adjacent Set Bits
- Count binary strings with k times appearing adjacent two set bits
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Find all even length binary sequences with same sum of first and second half bits
- Maximize count of set bits in a root to leaf path in a binary tree
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Maximum number of set bits count in a K-size substring of a Binary String
- Add n binary strings
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Bitwise AND of N binary strings
- XOR two binary strings of unequal lengths
- Generate string with Hamming Distance as half of the hamming distance between strings A and B
- Count of non-overlapping sub-strings "101" and "010" in the given binary string
- Number of sub-strings in a given binary string divisible by 2
- Count of binary strings of given length consisting of at least one 1
- Count number of binary strings without consecutive 1’s : Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

