# Find the number of valid parentheses expressions of given length

Given a number n find the number of valid parentheses expressions of that length.
Examples :

```Input: 2
Output: 1
There is only possible valid expression of length 2, "()"

Input: 4
Output: 2
Possible valid expression of length 4 are "(())" and "()()"

Input: 6
Output: 5
Possible valid expressions are ((())), ()(()), ()()(), (())() and (()())```

This is mainly an application of Catalan Numbers. Total possible valid expressions for input n is n/2’th Catalan Number if n is even and 0 if n is odd.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Below given is the implementation :

## C++

 `// C++ program to find valid paranthesisations of length n ` `// The majority of code is taken from method 3 of ` `// https://www.geeksforgeeks.org/program-nth-catalan-number/ ` `#include ` `using` `namespace` `std; ` ` `  `// Returns value of Binomial Coefficient C(n, k) ` `unsigned ``long` `int` `binomialCoeff(unsigned ``int` `n, ` `                                ``unsigned ``int` `k) ` `{ ` `    ``unsigned ``long` `int` `res = 1; ` ` `  `    ``// Since C(n, k) = C(n, n-k) ` `    ``if` `(k > n - k) ` `        ``k = n - k; ` ` `  `    ``// Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] ` `    ``for` `(``int` `i = 0; i < k; ++i) { ` `        ``res *= (n - i); ` `        ``res /= (i + 1); ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// A Binomial coefficient based function to  ` `// find nth catalan number in O(n) time ` `unsigned ``long` `int` `catalan(unsigned ``int` `n) ` `{ ` `    ``// Calculate value of 2nCn ` `    ``unsigned ``long` `int` `c = binomialCoeff(2 * n, n); ` ` `  `    ``// return 2nCn/(n+1) ` `    ``return` `c / (n + 1); ` `} ` ` `  `// Function to find possible ways to put balanced ` `// parenthesis in an expression of length n ` `unsigned ``long` `int` `findWays(unsigned n) ` `{ ` `    ``// If n is odd, not possible to  ` `    ``// create any valid parentheses ` `    ``if` `(n & 1) ` `        ``return` `0; ` ` `  `    ``// Otherwise return n/2'th Catalan Numer ` `    ``return` `catalan(n / 2); ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` `    ``int` `n = 6; ` `    ``cout << ``"Total possible expressions of length "` `         ``<< n << ``" is "` `<< findWays(6); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find valid paranthesisations of length n ` `// The majority of code is taken from method 3 of ` `// https://www.geeksforgeeks.org/program-nth-catalan-number/ ` ` `  `class` `GFG { ` `     `  `    ``// Returns value of Binomial Coefficient C(n, k) ` `    ``static` `long` `binomialCoeff(``int` `n, ``int` `k) ` `    ``{ ` `        ``long` `res = ``1``; ` ` `  `        ``// Since C(n, k) = C(n, n-k) ` `        ``if` `(k > n - k) ` `            ``k = n - k; ` ` `  `        ``// Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] ` `        ``for` `(``int` `i = ``0``; i < k; ++i) { ` `            ``res *= (n - i); ` `            ``res /= (i + ``1``); ` `        ``} ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// A Binomial coefficient based function to  ` `    ``// find nth catalan number in O(n) time ` `    ``static` `long` `catalan(``int` `n) ` `    ``{ ` `        ``// Calculate value of 2nCn ` `        ``long` `c = binomialCoeff(``2` `* n, n); ` ` `  `        ``// return 2nCn/(n+1) ` `        ``return` `c / (n + ``1``); ` `    ``} ` ` `  `    ``// Function to find possible ways to put balanced ` `    ``// parenthesis in an expression of length n ` `    ``static` `long` `findWays(``int` `n) ` `    ``{ ` `        ``// If n is odd, not possible to ` `        ``// create any valid parentheses ` `        ``if` `((n & ``1``) != ``0``) ` `            ``return` `0``; ` ` `  `        ``// Otherwise return n/2'th Catalan Numer ` `        ``return` `catalan(n / ``2``); ` `    ``} ` ` `  `    ``// Driver program to test above functions ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``6``; ` `        ``System.out.println(``"Total possible expressions of length "` `+  ` `                                          ``n + ``" is "` `+ findWays(``6``)); ` `    ``} ` `} ` ` `  `// This code is contributed by Smitha Dinesh Semwal `

## Python3

 `# Python3 program to find valid ` `# paranthesisations of length n ` `# The majority of code is taken ` `# from method 3 of ` `# https:#www.geeksforgeeks.org/program-nth-catalan-number/ ` ` `  `# Returns value of Binomial  ` `# Coefficient C(n, k) ` `def` `binomialCoeff(n, k): ` `    ``res ``=` `1``; ` ` `  `    ``# Since C(n, k) = C(n, n-k) ` `    ``if` `(k > n ``-` `k): ` `        ``k ``=` `n ``-` `k; ` ` `  `    ``# Calculate value of [n*(n-1)*--- ` `    ``# *(n-k+1)] / [k*(k-1)*---*1] ` `    ``for` `i ``in` `range``(k):  ` `        ``res ``*``=` `(n ``-` `i); ` `        ``res ``/``=` `(i ``+` `1``); ` ` `  `    ``return` `int``(res); ` ` `  `# A Binomial coefficient based  ` `# function to find nth catalan   ` `# number in O(n) time ` `def` `catalan(n): ` `     `  `    ``# Calculate value of 2nCn ` `    ``c ``=` `binomialCoeff(``2` `*` `n, n); ` ` `  `    ``# return 2nCn/(n+1) ` `    ``return` `int``(c ``/` `(n ``+` `1``)); ` ` `  `# Function to find possible ` `# ways to put balanced parenthesis ` `# in an expression of length n ` `def` `findWays(n): ` `     `  `    ``# If n is odd, not possible to  ` `    ``# create any valid parentheses ` `    ``if``(n & ``1``): ` `        ``return` `0``; ` ` `  `    ``# Otherwise return n/2'th ` `    ``# Catalan Numer ` `    ``return` `catalan(``int``(n ``/` `2``)); ` ` `  `# Driver Code ` `n ``=` `6``; ` `print``(``"Total possible expressions of length"``,  ` `                       ``n, ``"is"``, findWays(``6``)); ` `     `  `# This code is contributed by mits `

## C#

 `// C# program to find valid paranthesisations ` `// of length n The majority of code is taken ` `// from method 3 of  ` `// https://www.geeksforgeeks.org/program-nth-catalan-number/ ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Returns value of Binomial ` `    ``// Coefficient C(n, k) ` `    ``static` `long` `binomialCoeff(``int` `n, ``int` `k) ` `    ``{ ` `        ``long` `res = 1; ` ` `  `        ``// Since C(n, k) = C(n, n-k) ` `        ``if` `(k > n - k) ` `            ``k = n - k; ` ` `  `        ``// Calculate value of [n*(n-1)*---* ` `        ``// (n-k+1)] / [k*(k-1)*---*1] ` `        ``for` `(``int` `i = 0; i < k; ++i) ` `        ``{ ` `            ``res *= (n - i); ` `            ``res /= (i + 1); ` `        ``} ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// A Binomial coefficient based function to  ` `    ``// find nth catalan number in O(n) time ` `    ``static` `long` `catalan(``int` `n) ` `    ``{ ` `         `  `        ``// Calculate value of 2nCn ` `        ``long` `c = binomialCoeff(2 * n, n); ` ` `  `        ``// return 2nCn/(n+1) ` `        ``return` `c / (n + 1); ` `    ``} ` ` `  `    ``// Function to find possible ways to put  ` `    ``// balanced parenthesis in an expression ` `    ``// of length n ` `    ``static` `long` `findWays(``int` `n) ` `    ``{ ` `        ``// If n is odd, not possible to ` `        ``// create any valid parentheses ` `        ``if` `((n & 1) != 0) ` `            ``return` `0; ` ` `  `        ``// Otherwise return n/2'th ` `        ``// Catalan Numer ` `        ``return` `catalan(n / 2); ` `    ``} ` ` `  `    ``// Driver program to test  ` `    ``// above functions ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 6; ` `        ``Console.Write(``"Total possible expressions"` `                       ``+ ``"of length "` `+ n + ``" is "`  `                                   ``+ findWays(6)); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal. `

## PHP

 ` ``\$n` `- ``\$k``) ` `        ``\$k` `= ``\$n` `- ``\$k``; ` ` `  `    ``// Calculate value of [n*(n-1)*--- ` `    ``// *(n-k+1)] / [k*(k-1)*---*1] ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$k``; ++``\$i``)  ` `    ``{ ` `        ``\$res` `*= (``\$n` `- ``\$i``); ` `        ``\$res` `/= (``\$i` `+ 1); ` `    ``} ` ` `  `    ``return` `\$res``; ` `} ` ` `  `// A Binomial coefficient  ` `// based function to find  ` `// nth catalan number in  ` `// O(n) time ` `function` `catalan(``\$n``) ` `{ ` `     `  `    ``// Calculate value of 2nCn ` `    ``\$c` `= binomialCoeff(2 * ``\$n``, ``\$n``); ` ` `  `    ``// return 2nCn/(n+1) ` `    ``return` `\$c` `/ (``\$n` `+ 1); ` `} ` ` `  `// Function to find possible ` `// ways to put balanced ` `// parenthesis in an expression  ` `// of length n ` `function` `findWays(``\$n``) ` `{ ` `     `  `    ``// If n is odd, not possible to  ` `    ``// create any valid parentheses ` `    ``if` `(``\$n` `& 1) ` `        ``return` `0; ` ` `  `    ``// Otherwise return n/2'th ` `    ``// Catalan Numer ` `    ``return` `catalan(``\$n` `/ 2); ` `} ` ` `  `    ``// Driver Code ` `    ``\$n` `= 6; ` `    ``echo` `"Total possible expressions of length "` `                    ``, ``\$n` `, ``" is "` `, findWays(6); ` `     `  `// This code is contributed by nitin mittal ` `?> `

Output:

`Total possible expressions of length 6 is 5`

Time Complexity: O(n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.