Why am I getting wrong answer

Hey @ayushagarwal20188 the problem is that you are missing the cases because your code is merging the two rightmost closed brackets pair in order to generate solution so it never merges the left ones hence missed few cases. For example when n = 3, your code doesnt print (())().
What you really need to do is keep the track of open and close brackets, we’ll intialize them as 0. And then we’ll recursively call our function until open bracket count is less than given n(because we want to generate n pairs.)
If open bracket count exceeds the close bracket count then we’ll put close bracket and call again for the remaining ones.

void generateParenthesis(int n, int openB, int closeB, string str) {
    if(closeB == n) {
        cout<<str<<endl;
        return;
    }
    if(openB > closeB) {
        generateParenthesis(n, openB, closeB+1, str+')');
    }
    if(openB < n) {
        generateParenthesis(n, openB+1, closeB, str+'(');
    }
}

Refer to this above written program for better understanding of the solution described above.
If you still have any queries feel free to ask them.