Question: Please write the code in C language Exercise 2.29 Making Parentheses String Balanced Given a string S that only consists of open parentheses ( and

Please write the code in C language Exercise 2.29 Making Parentheses StringPlease write the code in C language

Exercise 2.29 Making Parentheses String Balanced Given a string S that only consists of open parentheses "(" and close parentheses ")", you are asked to determine the minimum number of additional parentheses needed to make the parentheses string S balanced. Note that the additional parentheses can be added to any position of string S, and can be either open parentheses "(" or close parentheses ")". Formally speaking, a parentheses string S is balanced if and only if - S is an empty string, or - S=AB, where A and B are both balanced parentheses strings, or - S=(A), where A is a balanced parentheses string. Input The first line is an integer N(1N103), indicating the number of test cases. Each test case contains two lines. The first line is an integer m(0m200), the length of the parentheses string. The second line is a potentially unbalanced parentheses string. Output The minimum number of additional parentheses ("(" or ")") needed to make the corresponding parentheses string balanced, by adding them to any position of the string. You can use the format "printf(" %d ", result);" to output the result of each test case. Sample Input 4 3 (() 3 ((( 2 () 6 ))((0) Sample Output 1304 Explanation There are 4 test cases. For ((), adding one ) to the end to make it balanced: (()). For (( (, adding three ) to the end to make it balanced: ((())). For (), it is already balanced, no parenthesis is needed. For ))(((), adding two ( and two ) to make it balanced: (())(())()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!