Generate all binary permutations such that there are more 1's than 0's at every point in all permutations - GeeksQuiz
Generate all permutations of given length such that every permutation has more 1's than 0's at every point.
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.
Read full article from Generate all binary permutations such that there are more 1's than 0's at every point in all permutations - GeeksQuiz
Generate all permutations of given length such that every permutation has more 1's than 0's at every point.
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.
// 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);
}