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.
Algorithm:
1. Define a function named “binomialCoeff” that takes two unsigned integers n and k as input and returns an unsigned long integer.
2. Inside the “binomialCoeff” function:
a. Define an unsigned long integer named res and initialize it to 1.
b. If k is greater than n-k, set k to n-k.
c. For i from 0 to k-1, do the following:
i. Multiply res by (n-i).
ii. Divide res by (i+1).
d. Return res.
3. Define a function named “catalan” that takes an unsigned integer n as input and returns an unsigned long integer.
4. Inside the “catalan” function:
a. Calculate the value of 2nCn by calling the “binomialCoeff” function with arguments 2*n and n.
b. Return the value of 2nCn/(n+1).
5. Define a function named “findWays” that takes an unsigned integer n as input and returns an unsigned long integer.
6. Inside the “findWays” function:
a. If n is odd, return 0.
b. Otherwise, return the nth Catalan number by calling the “catalan” function with argument n/2.
7. Define the main function.
8. Define an integer named n with the value 6.
9. Call the “findWays” function with argument 6 and print the result.
Below given is the implementation :
C++
#include <bits/stdc++.h>
using namespace std;
unsigned long int binomialCoeff(unsigned int n,
unsigned int k)
{
unsigned long int res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
unsigned long int catalan(unsigned int n)
{
unsigned long int c = binomialCoeff(2 * n, n);
return c / (n + 1);
}
unsigned long int findWays(unsigned n)
{
if (n & 1)
return 0;
return catalan(n / 2);
}
int main()
{
int n = 6;
cout << "Total possible expressions of length "
<< n << " is " << findWays(6);
return 0;
}
|
Java
class GFG {
static long binomialCoeff( int n, int k)
{
long res = 1 ;
if (k > n - k)
k = n - k;
for ( int i = 0 ; i < k; ++i) {
res *= (n - i);
res /= (i + 1 );
}
return res;
}
static long catalan( int n)
{
long c = binomialCoeff( 2 * n, n);
return c / (n + 1 );
}
static long findWays( int n)
{
if ((n & 1 ) != 0 )
return 0 ;
return catalan(n / 2 );
}
public static void main(String[] args)
{
int n = 6 ;
System.out.println( "Total possible expressions of length " +
n + " is " + findWays( 6 ));
}
}
|
Python3
def binomialCoeff(n, k):
res = 1 ;
if (k > n - k):
k = n - k;
for i in range (k):
res * = (n - i);
res / = (i + 1 );
return int (res);
def catalan(n):
c = binomialCoeff( 2 * n, n);
return int (c / (n + 1 ));
def findWays(n):
if (n & 1 ):
return 0 ;
return catalan( int (n / 2 ));
n = 6 ;
print ( "Total possible expressions of length" ,
n, "is" , findWays( 6 ));
|
C#
using System;
class GFG {
static long binomialCoeff( int n, int k)
{
long res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
static long catalan( int n)
{
long c = binomialCoeff(2 * n, n);
return c / (n + 1);
}
static long findWays( int n)
{
if ((n & 1) != 0)
return 0;
return catalan(n / 2);
}
public static void Main()
{
int n = 6;
Console.Write( "Total possible expressions"
+ "of length " + n + " is "
+ findWays(6));
}
}
|
PHP
<?php
function binomialCoeff( $n , $k )
{
$res = 1;
if ( $k > $n - $k )
$k = $n - $k ;
for ( $i = 0; $i < $k ; ++ $i )
{
$res *= ( $n - $i );
$res /= ( $i + 1);
}
return $res ;
}
function catalan( $n )
{
$c = binomialCoeff(2 * $n , $n );
return $c / ( $n + 1);
}
function findWays( $n )
{
if ( $n & 1)
return 0;
return catalan( $n / 2);
}
$n = 6;
echo "Total possible expressions of length "
, $n , " is " , findWays(6);
?>
|
Javascript
<script>
function binomialCoeff(n, k)
{
let res = 1;
if (k > n - k)
k = n - k;
for (let i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
function catalan(n)
{
let c = binomialCoeff(2 * n, n);
return c / (n + 1);
}
function findWays(n)
{
if (n & 1)
return 0;
return catalan(n / 2);
}
let n = 6;
document.write( "Total possible expressions of length " +
n + " is " + findWays(6));
</script>
|
Output:
Total possible expressions of length 6 is 5
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Sachin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above