Generate all permutations of given length such that every permutation has more or equal 1’s than 0’s in all prefixes of the permutation.

Examples:

Input: len = 4 Output: 1111 1110 1101 1100 1011 1010 Note that a permutation like 0101 can not be in output because there are more 0's from index 0 to 2 in this permutation. Input: len = 3 Output: 111 110 101 Input: len = 2 Output: 11 10

Like permutation generation problems, recursion is the simplest approach to solve this. We start with an empty string, attach 1 to it and recur. While recurring, if we find more 1’s at any point, we append a 0 and make one more recursive call.

Below is the implementation:

## C++

`// C++ program to generate all permutations of 1's and 0's such that ` `// every permutation has more 1's than 0's at all indexes. ` `#include <iostream> ` `#include <cstring> ` `using` `namespace` `std; ` ` ` `// ones & zeroes --> counts of 1's and 0's in current string 'str' ` `// len ---> desired length of every permutation ` `void` `generate(` `int` `ones, ` `int` `zeroes, string str, ` `int` `len) ` `{ ` ` ` `// If length of current string becomes same as desired length ` ` ` `if` `(len == str.length()) ` ` ` `{ ` ` ` `cout << str << ` `" "` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Append a 1 and recur ` ` ` `generate(ones+1, zeroes, str+` `"1"` `, len); ` ` ` ` ` `// If there are more 1's, append a 0 as well, and recur ` ` ` `if` `(ones > zeroes) ` ` ` `generate(ones, zeroes+1, str+` `"0"` `, len); ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `string str = ` `""` `; ` ` ` `generate(0, 0, str, 4); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to generate all permutations of 1's and 0's such that ` `// every permutation has more 1's than 0's at all indexes. ` ` ` `class` `GFG { ` ` ` `// ones & zeroes --> counts of 1's and 0's in current string 'str' ` `// len ---> desired length of every permutation ` ` ` `static` `void` `generate(` `int` `ones, ` `int` `zeroes, String str, ` `int` `len) { ` ` ` `// If length of current string becomes same as desired length ` ` ` `if` `(len == str.length()) { ` ` ` `System.out.print(str + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Append a 1 and recur ` ` ` `generate(ones + ` `1` `, zeroes, str + ` `"1"` `, len); ` ` ` ` ` `// If there are more 1's, append a 0 as well, and recur ` ` ` `if` `(ones > zeroes) { ` ` ` `generate(ones, zeroes + ` `1` `, str + ` `"0"` `, len); ` ` ` `} ` ` ` `} ` ` ` `// Driver program to test above function ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `String str = ` `""` `; ` ` ` `generate(` `0` `, ` `0` `, str, ` `4` `); ` ` ` `} ` `} ` ` ` `/* This Java code is contributed by PrinciRaj1992*/` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to generate all permutations of 1's and 0's such that ` `# every permutation has more 1's than 0's at all indexes. ` ` ` `# ones & zeroes --> counts of 1's and 0's in current string 'str' ` `# len ---> desired length of every permutation ` `def` `generate(ones, zeroes, ` `str` `, len1): ` ` ` `# If length of current string becomes same as desired length ` ` ` `if` `(len1 ` `=` `=` `len` `(` `str` `)): ` ` ` `print` `(` `str` `,end ` `=` `" "` `) ` ` ` `return` ` ` ` ` `# Append a 1 and recur ` ` ` `generate(ones` `+` `1` `, zeroes, ` `str` `+` `"1"` `, len1) ` ` ` ` ` `# If there are more 1's, append a 0 as well, and recur ` ` ` `if` `(ones > zeroes): ` ` ` `generate(ones, zeroes` `+` `1` `, ` `str` `+` `"0"` `, len1) ` ` ` `# Driver program to test above function ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `str` `=` `"" ` ` ` `generate(` `0` `, ` `0` `, ` `str` `, ` `4` `) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` ` ` |

*chevron_right*

*filter_none*

## C#

`// C# program to generate all permutations of 1's and 0's such that ` `// every permutation has more 1's than 0's at all indexes. ` ` ` `using` `System; ` ` ` ` ` `public` `class` `GFG { ` ` ` `// ones & zeroes --> counts of 1's and 0's in current string 'str' ` `// len ---> desired length of every permutation ` ` ` `static` `void` `generate(` `int` `ones, ` `int` `zeroes, String str, ` `int` `len) { ` ` ` `// If length of current string becomes same as desired length ` ` ` `if` `(len == str.Length) { ` ` ` `Console.Write(str + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Append a 1 and recur ` ` ` `generate(ones + 1, zeroes, str + ` `"1"` `, len); ` ` ` ` ` `// If there are more 1's, append a 0 as well, and recur ` ` ` `if` `(ones > zeroes) { ` ` ` `generate(ones, zeroes + 1, str + ` `"0"` `, len); ` ` ` `} ` ` ` `} ` ` ` `// Driver program to test above function ` ` ` `public` `static` `void` `Main() { ` ` ` `String str = ` `""` `; ` ` ` `generate(0, 0, str, 4); ` ` ` `} ` `} ` ` ` `/* This Java code is contributed by 29AjayKumar*/` |

*chevron_right*

*filter_none*

**Output:**

1111 1110 1101 1100 1011 1010

This article is contributed by **Sachin**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

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:

- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Number of binary strings such that there is no substring of length ≥ 3
- Generate all cyclic permutations of a number
- Generate all permutations of a string that follow given constraints
- Generate permutations with only adjacent swaps allowed
- Iterative program to generate distinct Permutations of a String
- Number of permutations such that sum of elements at odd index and even index are equal
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Probability of getting more heads than tails when N biased coins are tossed
- Print all permutations of a number N greater than itself
- Python | Check if there are K consecutive 1's in a binary number
- Count of distinct permutations of every possible length of given string
- Count ways to generate N-length array with 0s, 1s, and 2s such that sum of all adjacent pairwise products is K
- Number of ways to split a binary number such that every part is divisible by 2
- Generate all binary strings from given pattern
- Generate all the binary strings of N bits
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Check if a binary string contains all permutations of length k