Matched Brackets ZCO12001

Matched Brackets ZC012001
I am getting 3 test cases wrong, I didn’t get where I am wrong.
Here is my code

n = int(input())
a = list(map(int, input().split()))
b = []
c = 0
d = []
e = []
g = []
flmb = 0
for i in range(len(a)):
    if(a[i] == 1):
        b.append(a[i])
        c += 1
    else:
        if(len(b) == 0):
            break
        else:
            d.append(len(b))
            b.pop()
            c -= 1
            g.append(i)
    if(len(b) == 0):
        e.append(i+1)
#print(b)
#print(d)
#print(e)
#print(g)
nd = max(d)
for i in range(len(d)):
    if(d[i] == nd):
        j = i
        break
fnd = g[j]
lmb = 0
if(len(e) == 1):
    lmb = e[0]
    flmb = 1
else:
    for i in range(len(e)-1):
        if(lmb < abs(e[i+1] - e[i])):
            lmb = abs(e[i+1] - e[i])
            flmb = e[i] + 1
print(nd, fnd, lmb, flmb)

Consider the test input:

8
1 1 2 2 1 2 1 2
1 Like

I am getting 2 2 2 5 as output

I know :slight_smile: It should be:

2 2 4 1
2 Likes

I am going wrong in e list :frowning_face:

Thanks for your time and help, at last it done. But it took much time.

1 Like