Print the balanced bracket expression using given brackets

Given four integers a, b, c and d which signifies the number of four types of brackets.

  1. “((“
  2. “()”
  3. “)(“
  4. “))”

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print balanced bracket
// expression if it is possible
void printBalancedExpression(int a, int b, int c, int d)
{
  
    // If the condition is met
    if ((a == d && a) || (a == 0 && c == 0 && d == 0)) {
  
        // Print brackets of type-1
        for (int i = 1; i <= a; i++)
            cout << "((";
  
        // Print brackets of type-3
        for (int i = 1; i <= c; i++)
            cout << ")(";
  
        // Print brackets of type-4
        for (int i = 1; i <= d; i++)
            cout << "))";
  
        // Print brackets of type-2
        for (int i = 1; i <= b; i++)
            cout << "()";
    }
  
    // If the condition is not met
    else
        cout << -1;
}
  
// Driver code
int main()
{
    int a = 3, b = 1, c = 4, d = 3;
    printBalancedExpression(a, b, c, d);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach 
class GFG 
      
    // Function to print balanced bracket 
    // expression if it is possible 
    static void printBalancedExpression(int a, 
                            int b, int c, int d) 
    
      
        // If the condition is met 
        if (((a == d) && (a != 0)) || 
        ((a == 0) && (c == 0) && (d == 0))) 
        
      
            // Print brackets of type-1 
            for (int i = 1; i <= a; i++) 
                System.out.print("(("); 
      
            // Print brackets of type-3 
            for (int i = 1; i <= c; i++) 
                System.out.print(")("); 
      
            // Print brackets of type-4 
            for (int i = 1; i <= d; i++) 
                System.out.print("))"); 
      
            // Print brackets of type-2 
            for (int i = 1; i <= b; i++) 
                System.out.print("()"); 
        
      
        // If the condition is not met 
        else
            System.out.print(-1); 
    
      
    // Driver code 
    public static void main(String args[]) 
    
        int a = 3, b = 1, c = 4, d = 3
        printBalancedExpression(a, b, c, d); 
    
  
// This code is contributed by Ryuga 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to print balanced bracket
# expression if it is possible
def printBalancedExpression(a, b, c, d):
  
    # If the condition is met
    if ((a == d and a) or 
        (a == 0 and c == 0 and d == 0)): 
  
        # Print brackets of type-1
        for i in range(1, a + 1):
            print("((", end = "")
  
        # Print brackets of type-3
        for i in range(1, c + 1):
            print(")(", end = "")
  
        # Print brackets of type-4
        for i in range(1, d + 1):
            print("))", end = "")
  
        # Print brackets of type-2
        for i in range(1, b + 1):
            print("()", end = "")
      
    # If the condition is not met
    else:
        print("-1")
  
# Driver code
if __name__ == "__main__":
  
    a, b, c, d = 3, 1, 4, 3
    printBalancedExpression(a, b, c, d)
  
# This code is contributed by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to print balanced bracket
// expression if it is possible
static void printBalancedExpression(int a, 
                        int b, int c, int d)
{
  
    // If the condition is met
    if (((a == d) && (a != 0)) || 
       ((a == 0) && (c == 0) && (d == 0))) 
    {
  
        // Print brackets of type-1
        for (int i = 1; i <= a; i++)
            Console.Write("((");
  
        // Print brackets of type-3
        for (int i = 1; i <= c; i++)
            Console.Write(")(");
  
        // Print brackets of type-4
        for (int i = 1; i <= d; i++)
            Console.Write("))");
  
        // Print brackets of type-2
        for (int i = 1; i <= b; i++)
            Console.Write("()");
    }
  
    // If the condition is not met
    else
        Console.Write(-1);
}
  
// Driver code
public static void Main()
{
    int a = 3, b = 1, c = 4, d = 3;
    printBalancedExpression(a, b, c, d);
}
}
  
// This code is contributed by Akanksha Rai

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
//PHP implementation of the approach
// Function to print balanced bracket
// expression if it is possible
function printBalancedExpression($a, $b, $c, $d)
{
  
    // If the condition is met
    if (($a == $d && $a) || 
       ($a == 0 && $c == 0 && $d == 0)) 
    {
  
        // Print brackets of type-1
        for ($i = 1; $i <= $a; $i++)
            echo "((";
  
        // Print brackets of type-3
        for ($i = 1; $i <= $c; $i++)
            echo ")(";
  
        // Print brackets of type-4
        for ($i = 1; $i <= $d; $i++)
            echo "))";
  
        // Print brackets of type-2
        for ($i = 1; $i <= $b; $i++)
            echo "()";
    }
  
    // If the condition is not met
    else
        echo -1;
}
  
    // Driver code
    $a = 3;
    $b = 1;
    $c = 4;
    $d = 3;
    printBalancedExpression($a, $b, $c, $d);
      
    // This code is contributed by jit_t
  
?>

chevron_right


Output:

(((((()()()()())))))()


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.