Given four integers a, b, c and d which signifies the number of four types of brackets.
The task is to print any balanced bracket expression using all the given brackets. If we cannot form a balanced bracket expression then print -1. In case of multiple answers, print any one.
Input: a = 3, b = 1, c = 4, d = 3
Input: a = 3, b = 1, c = 4, d = 8
No balanced bracket expression is possible with the given brackets.
Approach: First check if the balanced bracket expression can be formed with the given number of brackets. We can form the expression if the number of type1 brackets is equal to the number of type4 brackets with any number of type3 brackets or if there are only type 2 brackets. Hence the combining condition will be:
(a == d && a) || (a == 0 && c == 0 && d == 0)
The following steps can be followed to print the balanced bracket expression if the above condition is satisfied:
- Print number of type1 brackets.
- Print number of type3 brackets.
- Print number of type4 brackets.
- Print number of type2 brackets.
Below is the implementation of the above approach:
- Number of balanced bracket subsequence of length 2 and 4
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Print Bracket Number
- Print all combinations of balanced parentheses
- Balance a string after removing extra brackets
- Balanced Prime
- Expression Evaluation
- Maximize the value of the given expression
- Pairs involved in Balanced Parentheses
- Check if concatenation of two strings is balanced or not
- Find all possible outcomes of a given expression
- Count Balanced Binary Trees of Height h
- Maximum and Minimum Values of an Algebraic Expression
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.