Number of balanced bracket subsequence of length 2 and 4

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 length 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Output:

14


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.