On submitting on coding blocks its showing wrong answer but its working on other compilers.
Here is my code. Also, i have printed the o/p in the correct order , i.e “(” has smaller value than “)”.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void generateBrackets(char out, int n, int idx, int open, int close) {
if(idx == 2n) {
out[idx] = ‘\0’;
cout << out << endl;
return;
}
if(close < open) {
out[idx] = ‘)’;
generateBrackets(out, n, idx + 1, open, close + 1);
}
if(open < n) {
out[idx] = ‘(’;
generateBrackets(out, n, idx + 1, open + 1, close);
}
return;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”,“r”, stdin);
freopen(“output.txt”,“w”, stdout);
#endif
int n; cin >> n;
char out[1000];
int idx = 0;
generateBrackets(out, n, 0, 0, 0);
return 0;
}