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` |

## 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);` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `a[] = { ` `1` `, ` `2` `, ` `1` `, ` `1` `, ` `2` `, ` `2` `};` ` ` `int` `n = ` `6` `;` ` ` `countWays(a, n);` ` ` `}` `}` |

## 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` |

## 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` |

## Javascript

`<script>` ` ` `// Javascript implementation of above approach` ` ` ` ` `function` `countWays(a, n)` ` ` `{` ` ` `let i, j;` ` ` `let suff = ` `new` `Array(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` ` ` `let 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];` ` ` `}` ` ` `}` ` ` `}` ` ` `document.write(ss);` ` ` `}` ` ` `let a = [ 1, 2, 1, 1, 2, 2 ];` ` ` `let n = 6;` ` ` `countWays(a, n);` ` ` ` ` `// This code is contributed by divyeshrabadiya07.` `</script>` |

**Output**

14

**Time Complexity:** O(N*N)**Auxiliary complexity :** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**