Given an integer n and an array of positions ‘position[]’ (1 <= position[i] <= 2n), find the number of ways of proper bracket expressions that can be formed of length 2n such that given positions have opening bracket.
**Examples :**

Input : n = 3, position[] = [2} Output : 3 Explanation : The proper bracket sequences of length 6 and opening bracket at position 2 are: [ [ ] ] [ ] [ [ [ ] ] ] [ [ ] [ ] ] Input : n = 2, position[] = {1, 3} Output : 1 Explanation: The only possibility is: [ ] [ ]

**Approach : ** This problem can be solved by **Dynamic programming.**.

Let **DP _{i, j} **be the number of valid ways of filling the first i positions such that there are j more brackets of type ‘[‘ than of type ‘]’. Valid ways would mean that it is the prefix of a matched bracket expression and that the locations at which enforced ‘[‘ brackets are enforced, have been satisfied. It is easy to see that DP

_{2N, 0}is the final answer.

The base case of the DP is, ** DP _{0, 0}=1.** We need to fill the first position with a ‘[‘ bracket, and there is only way to do this.

If the position **has a opening bracket sequence** which can be marked by a hash array, then the recurrence occurs as :

if(j != 0) dp_{i, j = dpi-1, j-1 else dpi, j = 0; }

If the position has **no opening bracket sequence**, then **recurrence** happens as :

if(j != 0) dp_{i, j}= dp_{i - 1, j - 1}+ dp_{i - 1, j + 1}else dp_{i, j}= dp_{i - 1, j + 1}

The answer will be** DP _{2n, 0}**

Given below is the CPP implementation of the above approach :

## C++

`// CPP code to find number of ways of ` `// arranging bracket with proper expressions ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define N 1000 ` ` ` `// function to calculate the number ` `// of proper bracket sequence ` `long` `long` `arrangeBraces(` `int` `n, ` `int` `pos[], ` `int` `k) ` `{ ` ` ` ` ` `// hash array to mark the ` ` ` `// positions of opening brackets ` ` ` `bool` `h[N]; ` ` ` ` ` `// dp 2d array ` ` ` `int` `dp[N][N]; ` ` ` ` ` `memset` `(h, 0, ` `sizeof` `h); ` ` ` `memset` `(dp, 0, ` `sizeof` `dp); ` ` ` ` ` `// mark positions in hash array ` ` ` `for` `(` `int` `i = 0; i < k; i++) ` ` ` `h[pos[i]] = 1; ` ` ` ` ` `// first position marked as 1 ` ` ` `dp[0][0] = 1; ` ` ` ` ` `// iterate and formulate the recurrences ` ` ` `for` `(` `int` `i = 1; i <= 2 * n; i++) { ` ` ` `for` `(` `int` `j = 0; j <= 2 * n; j++) { ` ` ` ` ` `// if position has a opening bracket ` ` ` `if` `(h[i]) { ` ` ` `if` `(j != 0) ` ` ` `dp[i][j] = dp[i - 1][j - 1]; ` ` ` `else` ` ` `dp[i][j] = 0; ` ` ` `} ` ` ` `else` `{ ` ` ` `if` `(j != 0) ` ` ` `dp[i][j] = dp[i - 1][j - 1] + ` ` ` `dp[i - 1][j + 1]; ` ` ` `else` ` ` `dp[i][j] = dp[i - 1][j + 1]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// return answer ` ` ` `return` `dp[2 * n][0]; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `// positions where opening braces ` ` ` `// will be placed ` ` ` `int` `pos[] = { 2 }; ` ` ` `int` `k = ` `sizeof` `(pos)/` `sizeof` `(pos[0]); ` ` ` ` ` `cout << arrangeBraces(n, pos, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

3

**Time Complexity:** O(n^2)

**Auxiliary Space:** O(n^2)

## Recommended Posts:

- Printing brackets in Matrix Chain Multiplication Problem
- Count Balanced Binary Trees of Height h
- Length of Longest Balanced Subsequence
- Balanced expressions such that given positions have opening brackets | Set 2
- Number of ways to partition a string into two balanced subsequences
- Number of balanced bracket expressions that can be formed from a string
- Number of Paths of Weight W in a K-ary tree
- Number of balanced bracket expressions that can be formed from a string
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally
- Ways to place K bishops on an N×N chessboard so that no two attack
- Find the sum of first N odd Fibonacci numbers
- Number of ways to partition a string into two balanced subsequences
- Minimum cost to reach a point N from 0 with two different operations allowed

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.