You are not logged in. Please login at to post your questions!


Solution for Matched Brackets problem of ZCO 2012

Hey, I was solving this question using JAVA, but I got a WA in the second last test case of the first sub-task and the last test case of the second sub task. Rest all 13 test cases were AC. Can someone look at my code, I have literally been scratching my head for two hours.

Problem page:

Code submission and test result:

Problem Statement -

A sequence of opening and closing brackets is well-bracketed if we can pair up each opening bracket with a matching closing bracket in the usual sense. For instance, the sequences (), (()) and ()(()) are well-bracketed, while (, ()), (()(), and )( are not well-bracketed.

The nesting depth of a well-bracketed sequence tells us the maximum number of levels of inner matched brackets enclosed within outer matched brackets. For instance, the nesting depth of () and ()()() is 1, the nesting depth of (()) and ()(()) is 2, the nesting depth of ((())) is 3, and so on.

Given a well-bracketed sequence, we are interested in computing the following:

The nesting depth, and the first position where it occurs–this will be the position of the first opening bracket at this nesting depth, where the positions are numbered starting with 1.

The maximum number of symbols between any pair of matched brackets, including both the outer brackets, and the first position where this occurs–that is, the position of the first opening bracket of this segment

For instance, the nesting depth of ()(())()(()())(()()) is 2 and the first position where this occurs is 4. The opening bracket at position 10 is also at nesting depth 2 but we have to report the first position where this occurs, which is 4.

In this sequence, the maximum number of symbols between a pair of matched bracket is 6, starting at position 9. There is another such sequence of length 6 starting at position 15, but this is not the first such position.

Input format

The input consists of two lines. The first line is a single integer N, the length of the bracket sequence. Positions in the sequence are numbered 1,2,…,N. The second line is a sequence of N space-separated integers that encode the bracket expression as follows: 1 denotes an opening bracket ( and 2 denotes a closing bracket ). Nothing other than 1 or 2 appears in the second line of input and the corresponding expression is guaranteed to be well-bracketed.

Output format

Your program should print 4 space-separated integers in a line, denoting the four quantities asked for in the following order: nesting depth, first position that achieves the nesting depth, length of the maximum sequence between matching brackets and the first position where such a maximum length sequence occurs.


You may assume that 2 ≤ N ≤ 105. In 30% of the test cases, 2 ≤ N ≤ 103. 
Subtask 1 (30 marks)
Subtask 2 (70 marks)

Sample Input

1 2 1 1 2 2 1 2 1 1 2 1 2 2 1 1 2 1 2 2

Sample Output

2 4 6 9

My code in JAVA -

import java.util.*;
class ZCO12001
    public static void main(String args[])
        Scanner sc=new Scanner(;
        int n=sc.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
        int c1=0;
        int c2=0;
        int s1=0;
        int s2=0;
        int h=0;
        int i=0;
            int c2x=0;
            int s2x=0;
        System.out.println(c1+" "+s1+" "+c2+" "+s2);

asked 28 Nov '16, 17:02

lord_of_codes's gravatar image

accept rate: 0%

edited 28 Nov '16, 17:04

@lord_of_codes You code is giving the wrong answer on this input:
1 2 1 2 1 2
correct answer for this test case would be 1 1 2 1 but your code is giving 0 0 2 1.
You just need to change the position of if condition where you are checking whether c1$<$h is true or not, place that at the start of inner while loop before changing the value of h.
Here, is the link of your AC code with corrections made.


answered 28 Nov '16, 18:15

srd091's gravatar image

accept rate: 42%

edited 28 Nov '16, 18:34


answered 19 May '17, 12:54

msd_007's gravatar image

accept rate: 5%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 28 Nov '16, 17:02

question was seen: 1,359 times

last updated: 19 May '17, 12:54