Generate paranthesis

#include
using namespace std;

void generate_bracket(char * out,int n,int idx,int open,int close){
//base case
if(idx==(2*n)){
out[idx]=’\0’;
cout<<out<<endl;
}
// recursive case-2 option
if(open<n){
out[idx]=’(’;
generate_bracket(out,n,idx+1,open+1,close);
}
if(close<open){
out[idx]=’)’;
generate_bracket(out,n,idx+1,open,close+1);
}

return;
}
int main() {
int n;
cin>>n;
char out[1000];
int ind=0;
generate_bracket(out,n,0,0,0);
return 0;
}

this is my code but it not pass the test case please tell what is the problme in it

Hey @sahudilip138

if(close<open){
		out[idx]=')';
		generate_bracket(out,n,idx+1,open,close+1);
	}
    if(open<n){
		out[idx]='(';
		generate_bracket(out,n,idx+1,open+1,close);
	}

Do this all test cases will get passed.

i not get just change the ordering of condition make all test case pass even both are if conditions so they are check separately

If you swap these conditions, you will see different output format. So the change i told you to do will bring that output format which is expected. That’s why your program get accepted.

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.