InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Balanced Brackets Queries

Given a string A of length N consists of only ’(‘ and ’)’. Answer M queries (L, R), find the length of the maximum correct bracket subsequence of sequence AL , AL + 1 , … , AR.

Note: A sequence T is a subsequence of A if T can be generated by deleting some elements of A.

Input Format:

    First argument contains the string A
    Second argument contains a M x 2 grid B denoting queries

Output Format:

    return an integer array denoting the answers to the respective queries


    1 <= N,M <= 100000
    1 <= l <= r <= N

For Example:

Input 1:
    A = "())(())(())(" B = [[1, 1], [1, 3], [1, 12], [5,11]]
Output 1:
    [0, 2, 10, 6]
    [1,1] corresponds to "(" which has no correct bracket subsequence.
    [1,3] corresponds to "())" which has "()" as correct bracket subsequence.
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Balanced Brackets Queries on Interview Code Editor
Sign Up
to access hints and editorial solutions for Balanced Brackets Queries


Click here to start solving coding interview questions