Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Super balanced bracket problem solution

YASH PAL, 31 July 2024
In this HackerEarth Super balanced bracket problem solution you are provided a balanced bracket sequence S. A super balanced bracket is a balanced bracket sequence that has the following structure:
((((…..))))
Here, all the opening brackets must contain corresponding closing brackets and all opening brackets must appear before any closing brackets. For example, (()) and ((())) are super balanced sequence but ()() and (()()) are not the super balanced sequence because there exists an opening bracket that appears after a closing bracket.
Your task is to determine the size of the largest super balanced bracket subsequence that is formed over all subsequences of the original sequence.
HackerEarth Super balanced bracket problem solution

HackerEarth Super balanced bracket problem solution.

#include <bits/stdc++.h>

typedef long double ld ;
#define long long long int
using namespace std;
#define mp make_pair
#define pb push_back
#define x first
#define y second
typedef pair<long,long> pll;

const int N=1e5+1;
int arr[N];

void check_balance(string &s)
{
int coun=0;
int x=s.length();

for(int i=0;i<x;i++)
{
if(s[i]=='(')
coun++;
else
coun--;
assert(coun>=0);
}
assert(coun==0);
}

int main()
{
ios::sync_with_stdio(false);cin.tie(0);
srand(time(NULL));

// clock_t clk = clock();

int t=1;
cin>>t;
while(t--)
{
string s;
cin>>s;

int x=s.length();
// cout<<x<<endl;
assert(1<=x && x<N);
check_balance(s);
arr[x]=0;

for(int i=x-1;i>=0;i--)
arr[i]=arr[i+1]+(s[i]==')');

int coun=0;
for(int i=0;i<x;i++)
{
if(s[i]=='(' && coun+1<=arr[i+1])
coun++;
}

cout<<2*coun<<"n";

// clk = clock() - clk;
// cout << "Time Elapsed: " << fixed << setprecision(10) << ((double)clk)/CLOCKS_PER_SEC << "n";
}

return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 14;

int main(){
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while(t--){
string s;
cin >> s;
int o = 0, cl = count(s.begin(), s.end(), ')'), ans = 0;
for(auto c : s)
if(c == '(')
ans = max(ans, min(++o, cl) * 2);
else
cl--;
cout << ans << 'n';
}
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes