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.
Examples:
Input: a = 3, b = 1, c = 4, d = 3
Output: (((((()()()()())))))()
Input: a = 3, b = 1, c = 4, d = 8
Output: -1
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:
C++
#include <bits/stdc++.h>
using namespace std;
void printBalancedExpression( int a, int b, int c, int d)
{
if ((a == d && a) || (a == 0 && c == 0 && d == 0)) {
for ( int i = 1; i <= a; i++)
cout << "((" ;
for ( int i = 1; i <= c; i++)
cout << ")(" ;
for ( int i = 1; i <= d; i++)
cout << "))" ;
for ( int i = 1; i <= b; i++)
cout << "()" ;
}
else
cout << -1;
}
int main()
{
int a = 3, b = 1, c = 4, d = 3;
printBalancedExpression(a, b, c, d);
return 0;
}
|
Java
class GFG
{
static void printBalancedExpression( int a,
int b, int c, int d)
{
if (((a == d) && (a != 0 )) ||
((a == 0 ) && (c == 0 ) && (d == 0 )))
{
for ( int i = 1 ; i <= a; i++)
System.out.print( "((" );
for ( int i = 1 ; i <= c; i++)
System.out.print( ")(" );
for ( int i = 1 ; i <= d; i++)
System.out.print( "))" );
for ( int i = 1 ; i <= b; i++)
System.out.print( "()" );
}
else
System.out.print(- 1 );
}
public static void main(String args[])
{
int a = 3 , b = 1 , c = 4 , d = 3 ;
printBalancedExpression(a, b, c, d);
}
}
|
Python3
def printBalancedExpression(a, b, c, d):
if ((a = = d and a) or
(a = = 0 and c = = 0 and d = = 0 )):
for i in range ( 1 , a + 1 ):
print ( "((" , end = "")
for i in range ( 1 , c + 1 ):
print ( ")(" , end = "")
for i in range ( 1 , d + 1 ):
print ( "))" , end = "")
for i in range ( 1 , b + 1 ):
print ( "()" , end = "")
else :
print ( "-1" )
if __name__ = = "__main__" :
a, b, c, d = 3 , 1 , 4 , 3
printBalancedExpression(a, b, c, d)
|
C#
using System;
class GFG
{
static void printBalancedExpression( int a,
int b, int c, int d)
{
if (((a == d) && (a != 0)) ||
((a == 0) && (c == 0) && (d == 0)))
{
for ( int i = 1; i <= a; i++)
Console.Write( "((" );
for ( int i = 1; i <= c; i++)
Console.Write( ")(" );
for ( int i = 1; i <= d; i++)
Console.Write( "))" );
for ( int i = 1; i <= b; i++)
Console.Write( "()" );
}
else
Console.Write(-1);
}
public static void Main()
{
int a = 3, b = 1, c = 4, d = 3;
printBalancedExpression(a, b, c, d);
}
}
|
PHP
<?php
function printBalancedExpression( $a , $b , $c , $d )
{
if (( $a == $d && $a ) ||
( $a == 0 && $c == 0 && $d == 0))
{
for ( $i = 1; $i <= $a ; $i ++)
echo "((" ;
for ( $i = 1; $i <= $c ; $i ++)
echo ")(" ;
for ( $i = 1; $i <= $d ; $i ++)
echo "))" ;
for ( $i = 1; $i <= $b ; $i ++)
echo "()" ;
}
else
echo -1;
}
$a = 3;
$b = 1;
$c = 4;
$d = 3;
printBalancedExpression( $a , $b , $c , $d );
?>
|
Javascript
<script>
function printBalancedExpression(a , b , c , d)
{
if (((a == d) && (a != 0)) || ((a == 0) &&
(c == 0) && (d == 0))) {
for (i = 1; i <= a; i++)
document.write( "((" );
for (i = 1; i <= c; i++)
document.write( ")(" );
for (i = 1; i <= d; i++)
document.write( "))" );
for (i = 1; i <= b; i++)
document.write( "()" );
}
else
document.write(-1);
}
var a = 3, b = 1, c = 4, d = 3;
printBalancedExpression(a, b, c, d);
</script>
|
Output:
(((((()()()()())))))()
Time Complexity: O(max(a,b,c,d)), as we are using loop to traverse a, b, c, and d times.
Auxiliary Space: O(1), as we are not using any extra space.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Jun, 2022
Like Article
Save Article