Have the function BracketCombinations(num) read num which will be an integer greater than or equal to zero, and return the number of valid combinations that can be formed with num pairs of parentheses.
For example, if the input is 3, then the possible combinations of 3 pairs of parenthesis, namely: ()()(), are ()()(), ()(()), (())(), ((())), and (()()). There are 5 total combinations when the input is 3, so your program should return 5.
Solution:
#include <iostream>
#include <string>
using namespace std;
int BracketCombinations(int num) {
if (num<=1)return 1;
long count=0;
for(int i=0;i<num;i++){
count+=(BracketCombinations(i)*BracketCombinations(num-i-1));
}
return count;
}
int main(void) {
cout << BracketCombinations(coderbyteInternalStdinFunction(stdin));
return 0;
}