Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
stack<char> st;
int count = 0;
for(auto i : s){
if(i == ')' && !st.empty()){
st.pop();
continue;
}
if(i == ')' && st.empty()){
count++;
continue;
}
st.push(i);
}
cout<<count<<endl;
}
}
Copy The Code &
Try With Live Editor
Input
4
2
)(
4
()()
8
())()()(
10
)))((((())
2
)(
4
()()
8
())()()(
10
)))((((())
Output
1
0
1
3
0
1
3
Demonstration
Codeforcess solution 1374-C C. Move Brackets ,C++, Java, Js and Python,1374-C,Codeforcess solution