Didn't understand the question

I am going through python practice problems.
There is a question like this:

" Given an integer ‘n’. Print all the possible pairs of ‘n’ balanced parentheses.
The output strings should be printed in the sorted order considering ‘(’ has higher value than ‘)’.

Input Format
Single line containing an integral value ‘n’.

Constraints
1<=n<=11

Output Format
Print the balanced parentheses strings with every possible solution on new line.

Sample Input
2
Sample Output
()()
(())
"
What is the logic behind the question, please explain."

what is the logic behind.

hey @hashmi846001 .

Approach: To form all the sequences of balanced bracket subsequences with n pairs. So there are n opening brackets and n closing brackets.
So the subsequence will be of length 2*n. There is a simple idea, the i’th character can be ‘(‘ if and only if the count of ‘(‘ till i’th is less than n and i’th character can be ‘)’ if and only if the count of ‘(‘ is greater than the count of ‘)’ till index i. If these two cases are followed then the resulting subsequence will always be balanced.
So form the recursive function using the above two cases.

I hope this simple explanation would help you work on this challenge.
Thank You and Happy learning :slight_smile:

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.