Skip to content

Commit

Permalink
Added solution for Parentheses Combination problem (aditya109#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
MashTerro authored and nitishanand99 committed Oct 14, 2021
1 parent b0d1b3b commit fa3d279
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Programming/C++/Generate Parenthesis/parenthesesCombinations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# include<iostream>
using namespace std;
# define MAX_COUNT 100
void printParenthesesPairs(int pos, int n, int open, int close){
static char str[MAX_COUNT];
if(close == n) {
cout<<str<<endl;
return;
}
else {
if(open > close) {
str[pos] = ')';
printParenthesesPairs(pos+1, n, open, close+1);
}
if(open < n) {
str[pos] = '(';
printParenthesesPairs(pos+1, n, open+1, close);
}
}
}
int main() {
int n;

cout<<"Enter a number for Parenthesis Combinations: ";
cin>>n;
if(n > 0)
printParenthesesPairs(0, n, 0, 0);
getchar();
return 0;
}

0 comments on commit fa3d279

Please sign in to comment.