Number of closing brackets needed to complete a regular bracket sequence | C ++ 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 ; Finding the length of sequence ; Counting opening brackets ; Counting closing brackets ; Checking if at any position the number of closing bracket is more then answer is impossible ; If possible , print ' s ' and required closing brackets . ; Driver code
#include <iostream> NEW_LINE using namespace std ; void completeSuquence ( string s ) { int n = s . length ( ) ; int open = 0 , close = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ( ' ) open ++ ; else close ++ ; if ( close > open ) { cout << " Impossible " << endl ; return ; } } cout << s ; for ( int i = 0 ; i < open - close ; i ++ ) cout << ' ) ' ; cout << endl ; } int main ( ) { string s = " ( ( ) ( ( ) ( " ; completeSuquence ( s ) ; return 0 ; }