Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Added solution for Parentheses Combination problem #551

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}