Generate the paranthese

sir pls tell what is wrong in code it is placing one extra bracket pls optimise it link to code is

Sorry for the delay.I’ll reply you by 10 pm tomorrow.

#include
using namespace std;
void generate(int n,int counto,int countc,int pos){
static char str[10000000]; //modulate the size as per question
if(countc==n){
cout<<str<<endl;
return;
}
if(counto<n){
str[pos] = ‘(’;
generate(n,counto+1,countc,pos+1);
}
if(countc<counto){
str[pos] = ‘)’;
generate(n,counto,countc+1,pos+1);
}
}
int main() {
int n;
cin>>n;
generate(n,0,0,0);
}

I have made a little change in your program i.e. to keep track of position.
i would suggest you to dry run your and my code on input 2.
you’ll definitely find the difference in the flow of execution or the way parentheses are getting stored.

Hopefully, this would help.

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.