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

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**.

Below is the implementation of the above approach:

## 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); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

(()(()()))

## Recommended Posts:

- Find index of closing bracket for a given opening bracket in an expression
- Minimum number of bracket reversals needed to make an expression balanced
- Minimum sum possible of any bracket sequence of length N
- Print Bracket Number
- Number of balanced bracket subsequence of length 2 and 4
- Number of balanced bracket expressions that can be formed from a string
- Least number of manipulations needed to ensure two strings have identical characters
- Minimum number of Appends needed to make a string palindrome
- Minimum number of days required to complete the work
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Form minimum number from given sequence
- Number of words in a camelcase sequence
- Check if two expressions with brackets are same
- Expression contains redundant bracket or not
- Binary tree to string with brackets

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.