Count distinct regular bracket sequences which are not N periodic
Last Updated :
20 Aug, 2021
Given an integer N, the task is to find the number of distinct bracket sequences that can be formed using 2 * N brackets such that the sequence is not N-periodic.
A bracket sequence str of length 2 * N is said to be N-periodic if the sequence can be split into two equal substrings having same regular bracket sequence.
A regular bracket sequence is a sequence in the following way:
- An empty string is a regular bracket sequence.
- If s & t are regular bracket sequences, then s + t is a regular bracket sequence.
Examples:
Input: N = 3
Output: 5
Explanation:
There will be 5 distinct regular bracket sequences of length 2 * N = ()()(), ()(()), (())(), (()()), ((()))
Now, none of the sequences are N-periodic. Therefore, the output is 5.
Input: N = 4
Output: 12
Explanation:
There will be 14 distinct regular bracket sequences of length 2*N which are
()()()(), ()()(()), ()(())(), ()(()()), ()((())), (())()(), (())(()), (()())(), (()()()), (()(())), ((()))(), ((())()), ((()())), (((())))
Out of these 14 regular sequences, two of them are N periodic which are
()()()() and (())(()). They have a period of N.
Therefore, the distinct regular bracket sequences of length 2 * N which are not N-periodic are 14 – 2 = 12.
Approach: The idea is to calculate the total number of regular bracket sequences possible of length 2 * N and then to subtract the number of bracket sequences which are N-periodic from it. Below are the steps:
- To find the number of regular bracket sequences of length 2*N, use the Catalan number formula.
- For a sequence of length 2*N to be N periodic, N should be even because if N is odd then the sequence of length 2*N cannot be a regular sequence and have a period of N at the same time.
- Since the concatenation of two similar non-regular bracket sequences cannot make a sequence regular, so both subsequences of length N should be regular.
- Reduce the number of regular bracket sequences of length N(if N is even) from the number of regular bracket sequences of length 2*N to get the desired result.
Below is the implementation of the above approach:
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);
}
void countNonNPeriodic( int N)
{
cout << findWays(2 * N)
- findWays(N);
}
int main()
{
int N = 4;
countNonNPeriodic(N);
return 0;
}
|
Java
import java.io.*;
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 ) == 1 )
return 0 ;
return catalan(n / 2 );
}
static void countNonNPeriodic( int N)
{
System.out.println(findWays( 2 * N) -
findWays(N));
}
public static void main (String[] args)
{
int N = 4 ;
countNonNPeriodic(N);
}
}
|
Python3
def binomialCoeff(n, k):
res = 1
if (k > n - k):
k = n - k
for i in range (k):
res = res * (n - i)
res = res / / (i + 1 )
return res
def catalan(n):
c = binomialCoeff( 2 * n, n)
return c / / (n + 1 )
def findWays(n):
if ((n & 1 ) = = 1 ):
return 0
return catalan(n / / 2 )
def countNonNPeriodic(N):
print (findWays( 2 * N) - findWays(N))
N = 4
countNonNPeriodic(N)
|
C#
using System;
using System.Collections.Generic;
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) == 1)
return 0;
return catalan(n / 2);
}
static void countNonNPeriodic( int N)
{
Console.Write(findWays(2 * N) -
findWays(N));
}
public static void Main( string [] args)
{
int N = 4;
countNonNPeriodic(N);
}
}
|
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);
}
function countNonNPeriodic(N)
{
document.write(findWays(2 * N)
- findWays(N));
}
let N = 4;
countNonNPeriodic(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...