-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatched_Brackets_ZCO12001.cpp
More file actions
46 lines (46 loc) · 1000 Bytes
/
Matched_Brackets_ZCO12001.cpp
File metadata and controls
46 lines (46 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
int a[n];
for (int i = 0; i < n; i++) {
cin>>a[i];
}
stack<int>s;
int max = 0, p;
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
s.push(a[i]);
if (max < s.size()) {
max = s.size();
p = i;
}
}
if (a[i] == 2 && !s.empty() && s.top() == 1) {
s.pop();
}
}
cout<<max<<" "<<p + 1<<" ";
stack<int>t;
int count = 0, maxi = 0, q;
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
t.push(a[i]);
count++;
}
if (a[i] == 2 && !t.empty() && t.top() == 1) {
t.pop();
count++;
}
if (t.empty()) {
if (maxi < count) {
maxi = count;
q = i;
}
count = 0;
}
}
cout<<maxi<<" "<<q - maxi + 2;
return 0;
}