Generate Parentheses

error-/bin/run.sh: line 4: 18 Killed ./exe

Hello @Anushka_Jha,

I have corrected your code:

Refer to comments for better understanding.

Hope, this would help.
Give a like if you are satisfied.

Thank you for correcting my code. in future i’ll make sure to add comment
i had a doubt that in the code i previously wrote i tried to apply a similar logic but it didn’t work . could you please tell me the mistakes i made in the previous code
if(ob>n) //when opening brackets are more than total number of pairs
return;
if(cb>ob) //When opening brackets are less than the closing brackets:
return;
if (ob==n &&cb==n) // when all opening and closing brackets are inserted
cout<<ans<<"\n";

 generate_bracket(ans+'(',ob++,cb,n);
generate_bracket(ans+')',ob,cb++,n);

Hello @Anushka_Jha,

  1. The only mistake i can see is the missing checks before the following statements:
    generate_bracket(ans+’(’,ob++,cb,n);
    generate_bracket(ans+’)’,ob,cb++,n);

    The checks should be the same as we have in my code.

  2. The base cases you have specified would work.
    But, you can reduce them to a single base case by:
    if (ob==n &&cb==n){ // when all opening and closing brackets are inserted
    cout<<ans<<"\n";
    //Add a return statement here:
    return;
    }

Hope, 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.