Optimal binary string recursion

not able to understand this…can you provide me the code…or any another video

hey @dreamerekta
Logic:
first try to place 0 or 1 at starting and call recursion to slove

now in recursion

// base case:
if (n == K) 
	{ 
		// terminate binary string 
		str[n] = '\0' ; 
		cout << str << "\n"; 
		return ; 
	} 

in recursive case you have to place 0 or 1 and call the recursion again

if previous character is '1' then we put 
only 0 at end of string 
example str = "01" then new string be "010" 

if previous character is '0' than we put 
both '1' and '0' at end of string 
example str = "00" then new string "001" and "000" 

complete code


i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved :grinning:
if you have more doubts regarding this feel free to ask

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Continuing the discussion from Optimal binary string recursion:

for any value of n, total binary strings will be 2^n. thus from 2^n strings, pick each string and check whether it has consecutive 1’s or not
//code
#include

#include<bits/stdc++.h>

using namespace std;

int cnt = 0;

bool consecutiveOnes(int i){

while(i>0){

    int no = i>>1;

    if(i&1 == 1 && no&1 == 1){

        return false;

    }

    i = i>>1;

}

return true;

}

int binary_strings(int n){

int len = pow(2,n);

for(int i = 0;i < len;i++){

    if(consecutiveOnes(i)){

        cnt++;

    }

}

return cnt;

}

int main(){

int n;

cin>>n;

cout<<binary_strings(n)<<endl;

return 0;

}

I know this is not the best optimised way to solve this question, but keeping in mind that no such big constraints are there, this can work