{"id":"43e88e8c-9012-478d-b499-8a2ab1ee818e","name":"Minimum Add To Make Parentheses Valid","description":"1: Given a string S of '(' and ')' parentheses.\r\n2: You need to find count of minimum number of parentheses '(' or ')' when added in any positions so that the resulting parentheses string is valid.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1: S.length &lt;= 1000\r\n2: S only consists of '(' and ')' characters.\r\n","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<stack>\nusing namespace std;\n\nint minAddToMakeValid(string s){\n // write your code here\n}\n\nint main(){\n string s;\n cin >> s;\n int ans = minAddToMakeValid(s);\n cout << ans << endl;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static int minAddToMakeValid(String S) {\r\n // complete this function\r\n return 0;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int result = minAddToMakeValid(read.readLine());\r\n System.out.println(result);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"()))((","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/e7HyTdHAf4U","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"001e12a2-b289-4229-af13-2336aeb4cb0a","name":"Minimum Add To Make Parentheses Valid","slug":"minimum-add-to-make-parentheses-valid","type":1}],"next":{"id":"e7edb4dd-3f1f-442c-9f73-3295888a58f6","name":"Minimum Add To Make Parentheses Valid Medium MCQ","type":0,"slug":"minimum-add-to-make-parentheses-valid-medium-mcq"},"prev":{"id":"0658adb8-d0a7-4718-8bce-df7cca5d4337","name":"Validate Stack Sequences Medium","type":3,"slug":"validate-stack-sequences-medium"}}}

Minimum Add To Make Parentheses Valid

1: Given a string S of '(' and ')' parentheses. 2: You need to find count of minimum number of parentheses '(' or ')' when added in any positions so that the resulting parentheses string is valid.

{"id":"43e88e8c-9012-478d-b499-8a2ab1ee818e","name":"Minimum Add To Make Parentheses Valid","description":"1: Given a string S of '(' and ')' parentheses.\r\n2: You need to find count of minimum number of parentheses '(' or ')' when added in any positions so that the resulting parentheses string is valid.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1: S.length &lt;= 1000\r\n2: S only consists of '(' and ')' characters.\r\n","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<stack>\nusing namespace std;\n\nint minAddToMakeValid(string s){\n // write your code here\n}\n\nint main(){\n string s;\n cin >> s;\n int ans = minAddToMakeValid(s);\n cout << ans << endl;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static int minAddToMakeValid(String S) {\r\n // complete this function\r\n return 0;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int result = minAddToMakeValid(read.readLine());\r\n System.out.println(result);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"()))((","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/e7HyTdHAf4U","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"001e12a2-b289-4229-af13-2336aeb4cb0a","name":"Minimum Add To Make Parentheses Valid","slug":"minimum-add-to-make-parentheses-valid","type":1}],"next":{"id":"e7edb4dd-3f1f-442c-9f73-3295888a58f6","name":"Minimum Add To Make Parentheses Valid Medium MCQ","type":0,"slug":"minimum-add-to-make-parentheses-valid-medium-mcq"},"prev":{"id":"0658adb8-d0a7-4718-8bce-df7cca5d4337","name":"Validate Stack Sequences Medium","type":3,"slug":"validate-stack-sequences-medium"}}}
plane

Editor


Loading...

Minimum Add To Make Parentheses Valid

medium

1: Given a string S of '(' and ')' parentheses. 2: You need to find count of minimum number of parentheses '(' or ')' when added in any positions so that the resulting parentheses string is valid.

Constraints

1: S.length <= 1000 2: S only consists of '(' and ')' characters.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

()))((

Sample Output

4

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode