Number of closing brackets needed to complete a regular bracket sequence Last Updated : 09 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an incomplete bracket sequence S. The task is to find the number of closing brackets ')' needed to make it a regular bracket sequence and print the complete bracket sequence. You are allowed to add the brackets only at the end of the given bracket sequence. If it is not possible to complete the bracket sequence, print "IMPOSSIBLE". Let us define a regular bracket sequence in the following way: Empty string is a regular bracket sequence.If s is a regular bracket sequence, then (s) is a regular bracket sequence.If s & t are regular bracket sequences, then st is a regular bracket sequence. Examples: Input : str = "(()(()(" Output : (()(()())) Explanation : The minimum number of ) needed to make the sequence regular are 3 which are appended at the end. Input : str = "())(()" Output : IMPOSSIBLE Approach : We need to add minimal number of closing brackets ')', so we will count the number of unbalanced opening brackets and then we will add that amount of closing brackets. If at any point the number of the closing bracket is greater than the opening bracket then the answer is IMPOSSIBLE. Algorithm : Create two variables open = 0 and close = 0Traverse on a string from i = 0 to i = n(size of string)If current element is '(' then increment open else if current element is ')' then increment close.While traversing check if count of close is greater than open or not if yes then print Impossible return to main After completion of traversal calculate (open-close) as that many times closing brackets required to make the sequence balanced. Below is the implementation of the above approach: C++ // C++ program to find number of closing // brackets needed and complete a regular // bracket sequence #include <iostream> using namespace std; // Function to find number of closing // brackets and complete a regular // bracket sequence void completeSequence(string s) { // Finding the length of sequence int n = s.length(); int open = 0, close = 0; for (int i = 0; i < n; i++) { // Counting opening brackets if (s[i] == '(') open++; else // Counting closing brackets close++; // Checking if at any position the // number of closing bracket // is more then answer is impossible if (close > open) { cout << "Impossible" << endl; return; } } // If possible, print 's' and // required closing brackets. cout << s; for (int i = 0; i < open - close; i++) cout << ')'; cout << endl; } // Driver code int main() { string s = "(()(()("; completeSequence(s); return 0; } // This code is contributed by // sanjeev2552 Java // Java program to find number of closing // brackets needed and complete a regular // bracket sequence class GFG { // Function to find number of closing // brackets and complete a regular // bracket sequence static void completeSequence(String s) { // Finding the length of sequence int n = s.length(); int open = 0, close = 0; for (int i = 0; i < n; i++) { // Counting opening brackets if (s.charAt(i) == '(') open++; else // Counting closing brackets close++; // Checking if at any position the // number of closing bracket // is more then answer is impossible if (close > open) { System.out.print("IMPOSSIBLE"); return; } } // If possible, print 's' and required closing // brackets. System.out.print(s); for (int i = 0; i < open - close; i++) System.out.print(")"); } // Driver code public static void main(String[] args) { String s = "(()(()("; completeSequence(s); } } Python 3 # Python 3 program to find number of # closing brackets needed and complete # a regular bracket sequence # Function to find number of closing # brackets and complete a regular # bracket sequence def completeSequence(s): # Finding the length of sequence n = len(s) open = 0 close = 0 for i in range(n): # Counting opening brackets if (s[i] == '('): open += 1 else: # Counting closing brackets close += 1 # Checking if at any position the # number of closing bracket # is more then answer is impossible if (close > open): print("IMPOSSIBLE") return # If possible, print 's' and # required closing brackets. print(s, end = "") for i in range(open - close): print(")", end = "") # Driver code if __name__ == "__main__": s = "(()(()(" completeSequence(s) # This code is contributed by ita_c C# // C# program to find number of closing // brackets needed and complete a // regular bracket sequence using System; class GFG { // Function to find number of closing // brackets and complete a regular // bracket sequence static void completeSequence(String s) { // Finding the length of sequence int n = s.Length; int open = 0, close = 0; for (int i = 0; i < n; i++) { // Counting opening brackets if (s[i] == '(') open++; else // Counting closing brackets close++; // Checking if at any position the // number of closing bracket // is more then answer is impossible if (close > open) { Console.Write("IMPOSSIBLE"); return; } } // If possible, print 's' and // required closing brackets. Console.Write(s); for (int i = 0; i < open - close; i++) Console.Write(")"); } // Driver Code static void Main() { String s = "(()(()("; completeSequence(s); } } // This code is contributed // by ANKITRAI1 PHP <?php // PHP program to find number of closing // brackets needed and complete a // regular bracket sequence // Function to find number of closing // brackets and complete a regular // bracket sequence function completeSequence($s) { // Finding the length of sequence $n = strlen($s); $open = 0; $close = 0; for ($i = 0; $i < $n; $i++) { // Counting opening brackets if ($s[$i] == '(') $open++; else // Counting closing brackets $close++; // Checking if at any position the // number of closing bracket // is more then answer is impossible if ($close > $open) { echo ("IMPOSSIBLE"); return; } } // If possible, print 's' and // required closing brackets. echo ($s); for ($i = 0; $i < $open - $close; $i++) echo (")"); } // Driver Code $s = "(()(()("; completeSequence($s); // This code is contributed // by ajit ?> JavaScript <script> // Javascript program to find number of closing // brackets needed and complete a // regular bracket sequence // Function to find number of closing // brackets and complete a regular // bracket sequence function completeSequence(s) { // Finding the length of sequence let n = s.length; let open = 0, close = 0; for (let i = 0; i < n; i++) { // Counting opening brackets if (s[i] == '(') open++; else // Counting closing brackets close++; // Checking if at any position the // number of closing bracket // is more then answer is impossible if (close > open) { document.write("IMPOSSIBLE"); return; } } // If possible, print 's' and // required closing brackets. document.write(s); for (let i = 0; i < open - close; i++) document.write(")"); } let s = "(()(()("; completeSequence(s); </script> Output(()(()())) Complexity Analysis: Time Complexity: O(n) , where n is size of given stringAuxiliary Space: O(1) , as we are not using any extra space. Comment More infoAdvertise with us Next Article Minimum number of Parentheses to be added to make it valid I indrajit1 Follow Improve Article Tags : DSA Parentheses-Problems Similar Reads Mastering Bracket Problems for Competitive Programming Bracket problems in programming typically refer to problems that involve working with parentheses, and/or braces in expressions or sequences. It typically refers to problems related to the correct and balanced usage of parentheses, and braces in expressions or code. These problems often involve chec 4 min read Check for balanced with only one type of brackets Given a string str of length N, consisting of '(' and ')' only, the task is to check whether it is balanced or not.Examples:Input: str = "((()))()()" Output: BalancedInput: str = "())((())" Output: Not Balanced Approach 1: Declare a Flag variable which denotes expression is balanced or not.Initialis 9 min read Valid Parentheses in an Expression Given a string s representing an expression containing various types of brackets: {}, (), and [], the task is to determine whether the brackets in the expression are balanced or not. A balanced expression is one where every opening bracket has a corresponding closing bracket in the correct order.Exa 8 min read Length of longest balanced parentheses prefix Given a string of open bracket '(' and closed bracket ')'. The task is to find the length of longest balanced prefix. Examples: Input : S = "((()())())((" Output : 10From index 0 to index 9, they are forming a balanced parentheses prefix.Input : S = "()(())((()"Output : 6The idea is take value of op 9 min read Modify a numeric string to a balanced parentheses by replacements Given a numeric string S made up of characters '1', '2' and '3' only, the task is to replace characters with either an open bracket ( '(' ) or a closed bracket ( ')' ) such that the newly formed string becomes a balanced bracket sequence. Note: All occurrences of a character must be replaced by the 10 min read Check if the bracket sequence can be balanced with at most one change in the position of a bracket Given an unbalanced bracket sequence as a string str, the task is to find whether the given string can be balanced by moving at most one bracket from its original place in the sequence to any other position.Examples: Input: str = ")(()" Output: Yes As by moving s[0] to the end will make it valid. "( 6 min read Number of closing brackets needed to complete a regular bracket sequence Given an incomplete bracket sequence S. The task is to find the number of closing brackets ')' needed to make it a regular bracket sequence and print the complete bracket sequence. You are allowed to add the brackets only at the end of the given bracket sequence. If it is not possible to complete th 7 min read Minimum number of Parentheses to be added to make it valid Given a string S of parentheses '(' or ')' where, 0\leq len(S)\leq 1000 . The task is to find a minimum number of parentheses '(' or ')' (at any positions) we must add to make the resulting parentheses string is valid. Examples: Input: str = "())" Output: 1 One '(' is required at beginning. Input: s 9 min read Minimum bracket reversals to make an expression balanced Given an expression with only '}' and '{'. The expression may not be balanced. Find minimum number of bracket reversals to make the expression balanced.Examples: Input: s = "}{{}}{{{"Output: 3Explanation: We need to reverse minimum 3 brackets to make "{{{}}{}}". Input: s = "{{"Output: 1Explanation: 15+ min read Find the number of valid parentheses expressions of given length Given a number n, the task is to find the number of valid parentheses expressions of that length. Examples : Input: 2Output: 1 Explanation: There is only possible valid expression of length 2, "()"Input: 4Output: 2 Explanation: Possible valid expression of length 4 are "(())" and "()()" Input: 6Outp 11 min read Like