Given a bracket sequence of even length. The task is to find how many ways are there to make balanced bracket subsequences from the given sequence of length 2 and 4.

The sequence () is a bracket sequence of length 2. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

**Note:** “1” represent opening bracket and “2” represents closing bracket

**Examples:**

Input: 1 2 1 1 2 2 Output: 14 Input: 1 2 1 2 Output: 4

**Approach:** For the **length 2 subsequence** we will just see for each 1 how many 2 are there which can be easily achievable by taking a simple suffix sum of the number of 2 ‘s present in the sequence. So first take the suffix sum of the number of 2 ‘s present in the sequence.

For l**ength 4 subsequence** there are 2 choices:

First one is 1 1 2 2

And the other one is 1 2 1 2.

- For the 1st one, run a loop from left to right to get the first open bracket an inner loop to get the next opening bracket now from the suffix array we can get the count of 2 after the 2nd opening bracket and calculate the number of subsequence by count*(count-1)/2 because for each closing bracket for inner opening bracket we get count-1 number of choices for the 1st opening bracket.
- For the 2nd type of subsequence, we again run a loop from left to right to get the first open bracket an inner loop to get the next opening bracket. Then we calculate the number of subsequence by getting the count of 2’s after the 1st opening bracket by simply subtracting the count of 2’s after the 2nd opening bracket and the count of 2’s after the 1st opening bracket and multiplying it with the count of 2’s after the 2nd opening bracket (we get all these values from the frequency suffix array).

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `countWays(` `int` `a[], ` `int` `n) ` ` ` `{ ` ` ` `int` `i, j; ` ` ` `long` `suff[n] ; ` ` ` `if` `(a[n - 1] == 2) ` ` ` `suff[n - 1] = 1; ` ` ` ` ` `// Taking the frequncy suffix sum of the ` ` ` `// number of 2's present after every index ` ` ` `for` `(i = n - 2; i >= 0; i--) ` ` ` `{ ` ` ` `if` `(a[i] == 2) ` ` ` `suff[i] = suff[i + 1] + 1; ` ` ` `else` ` ` `suff[i] = suff[i + 1]; ` ` ` `} ` ` ` ` ` `// Storing the count of subsequence ` ` ` `long` `ss = 0; ` ` ` ` ` `// Subsequence of length 2 ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(a[i] == 1) ` ` ` `ss += suff[i]; ` ` ` `} ` ` ` ` ` `// Subsequence of length 4 of type 1 1 2 2 ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `{ ` ` ` `for` `(j = i + 1; j < n; j++) ` ` ` `{ ` ` ` `if` `(a[i] == 1 && a[j] == 1 && suff[j] >= 2) ` ` ` `{ ` ` ` `ss += (suff[j]) * (suff[j] - 1) / 2; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Subsequence of length 4 of type 1 2 1 2 ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `{ ` ` ` `for` `(j = i + 1; j < n; j++) ` ` ` `{ ` ` ` `if` `(a[i] == 1 && a[j] == 1 && ` ` ` `(suff[i] - suff[j]) >= 1 && suff[j] >= 1) ` ` ` `{ ` ` ` `ss += (suff[i] - suff[j]) * suff[j]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `cout<<(ss); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `int` `main() ` ` ` `{ ` ` ` ` ` `int` `a[] = { 1, 2, 1, 1, 2, 2 }; ` ` ` `int` `n = 6; ` ` ` `countWays(a, n); ` ` ` `return` `0; ` ` ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `class` `GFG { ` ` ` ` ` `public` `static` `void` `countWays(` `int` `a[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `int` `i, j; ` ` ` `long` `suff[] = ` `new` `long` `[n]; ` ` ` `if` `(a[n - ` `1` `] == ` `2` `) ` ` ` `suff[n - ` `1` `] = ` `1` `; ` ` ` ` ` `// Taking the frequncy suffix sum of the ` ` ` `// number of 2's present after every index ` ` ` `for` `(i = n - ` `2` `; i >= ` `0` `; i--) { ` ` ` `if` `(a[i] == ` `2` `) ` ` ` `suff[i] = suff[i + ` `1` `] + ` `1` `; ` ` ` `else` ` ` `suff[i] = suff[i + ` `1` `]; ` ` ` `} ` ` ` ` ` `// Storing the count of subsequence ` ` ` `long` `ss = ` `0` `; ` ` ` ` ` `// Subsequence of length 2 ` ` ` `for` `(i = ` `0` `; i < n; i++) { ` ` ` `if` `(a[i] == ` `1` `) ` ` ` `ss += suff[i]; ` ` ` `} ` ` ` ` ` `// Subsequence of length 4 of type 1 1 2 2 ` ` ` `for` `(i = ` `0` `; i < n; i++) { ` ` ` `for` `(j = i + ` `1` `; j < n; j++) { ` ` ` `if` `(a[i] == ` `1` `&& a[j] == ` `1` `&& suff[j] >= ` `2` `) { ` ` ` `ss += (suff[j]) * (suff[j] - ` `1` `) / ` `2` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Subsequence of length 4 of type 1 2 1 2 ` ` ` `for` `(i = ` `0` `; i < n; i++) { ` ` ` `for` `(j = i + ` `1` `; j < n; j++) { ` ` ` `if` `(a[i] == ` `1` `&& a[j] == ` `1` `&& ` ` ` `(suff[i] - suff[j]) >= ` `1` `&& suff[j] >= ` `1` `) { ` ` ` `ss += (suff[i] - suff[j]) * suff[j]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `System.out.println(ss); ` ` ` `} ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `a[] = { ` `1` `, ` `2` `, ` `1` `, ` `1` `, ` `2` `, ` `2` `}; ` ` ` `int` `n = ` `6` `; ` ` ` `countWays(a, n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of ` `# above approach ` `def` `countWays(a, n): ` ` ` ` ` `suff ` `=` `[` `0` `] ` `*` `n ` ` ` `if` `(a[n ` `-` `1` `] ` `=` `=` `2` `): ` ` ` `suff[n ` `-` `1` `] ` `=` `1` ` ` ` ` `# Taking the frequncy suffix sum ` ` ` `# of the number of 2's present ` ` ` `# after every index ` ` ` `for` `i ` `in` `range` `(n ` `-` `2` `, ` `-` `1` `, ` `-` `1` `) : ` ` ` `if` `(a[i] ` `=` `=` `2` `): ` ` ` `suff[i] ` `=` `suff[i ` `+` `1` `] ` `+` `1` ` ` `else` `: ` ` ` `suff[i] ` `=` `suff[i ` `+` `1` `] ` ` ` ` ` `# Storing the count of subsequence ` ` ` `ss ` `=` `0` ` ` ` ` `# Subsequence of length 2 ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `if` `(a[i] ` `=` `=` `1` `): ` ` ` `ss ` `+` `=` `suff[i] ` ` ` ` ` `# Subsequence of length 4 of type 1 1 2 2 ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n) : ` ` ` `if` `(a[i] ` `=` `=` `1` `and` ` ` `a[j] ` `=` `=` `1` `and` `suff[j] >` `=` `2` `) : ` ` ` `ss ` `+` `=` `(suff[j]) ` `*` `(suff[j] ` `-` `1` `) ` `/` `/` `2` ` ` ` ` `# Subsequence of length 4 ` ` ` `# of type 1 2 1 2 ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n) : ` ` ` `if` `(a[i] ` `=` `=` `1` `and` `a[j] ` `=` `=` `1` `and` ` ` `(suff[i] ` `-` `suff[j]) >` `=` `1` `and` ` ` `suff[j] >` `=` `1` `): ` ` ` `ss ` `+` `=` `(suff[i] ` `-` `suff[j]) ` `*` `suff[j] ` ` ` ` ` `print` `(ss) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[ ` `1` `, ` `2` `, ` `1` `, ` `1` `, ` `2` `, ` `2` `] ` ` ` `n ` `=` `6` ` ` `countWays(a, n) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of ` `// above approach ` `using` `System; ` `class` `GFG ` `{ ` `public` `static` `void` `countWays(` `int` `[]a, ` ` ` `int` `n) ` `{ ` ` ` ` ` `int` `i, j; ` ` ` `long` `[]suff = ` `new` `long` `[n]; ` ` ` `if` `(a[n - 1] == 2) ` ` ` `suff[n - 1] = 1; ` ` ` ` ` `// Taking the frequncy suffix ` ` ` `// sum of the number of 2's ` ` ` `// present after every index ` ` ` `for` `(i = n - 2; i >= 0; i--) ` ` ` `{ ` ` ` `if` `(a[i] == 2) ` ` ` `suff[i] = suff[i + 1] + 1; ` ` ` `else` ` ` `suff[i] = suff[i + 1]; ` ` ` `} ` ` ` ` ` `// Storing the count of subsequence ` ` ` `long` `ss = 0; ` ` ` ` ` `// Subsequence of length 2 ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(a[i] == 1) ` ` ` `ss += suff[i]; ` ` ` `} ` ` ` ` ` `// Subsequence of length 4 ` ` ` `// of type 1 1 2 2 ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `{ ` ` ` `for` `(j = i + 1; j < n; j++) ` ` ` `{ ` ` ` `if` `(a[i] == 1 && a[j] == 1 && ` ` ` `suff[j] >= 2) ` ` ` `{ ` ` ` `ss += (suff[j]) * ` ` ` `(suff[j] - 1) / 2; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Subsequence of length 4 ` ` ` `// of type 1 2 1 2 ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `{ ` ` ` `for` `(j = i + 1; j < n; j++) ` ` ` `{ ` ` ` `if` `(a[i] == 1 && a[j] == 1 && ` ` ` `(suff[i] - suff[j]) >= 1 && ` ` ` `suff[j] >= 1) ` ` ` `{ ` ` ` `ss += (suff[i] - suff[j]) * ` ` ` `suff[j]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `Console.WriteLine(ss); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]a = { 1, 2, 1, 1, 2, 2 }; ` ` ` `int` `n = 6; ` ` ` `countWays(a, n); ` `} ` `} ` ` ` `// This code is contributed by Shashank ` |

*chevron_right*

*filter_none*

**Output:**

14

## Recommended Posts:

- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Find index of closing bracket for a given opening bracket in an expression
- Minimum number of bracket reversals needed to make an expression balanced
- Number of balanced bracket expressions that can be formed from a string
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Find the lexicographical next balanced bracket sequence
- Print the balanced bracket expression using given brackets
- Convert an unbalanced bracket sequence to a balanced sequence
- Minimum Cost required to generate a balanced Bracket Sequence
- Length of Longest Balanced Subsequence
- Range Queries for Longest Correct Bracket Subsequence
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Number of closing brackets needed to complete a regular bracket sequence
- Print Bracket Number
- Maximum length of balanced string after swapping and removal of characters
- Length of longest balanced parentheses prefix
- Print all ways to break a string in bracket form

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.