Given an integer n and an array of positions ‘position’ (1 <= length(position) <= 2n), find the number of ways of proper bracket expressions that can be formed of length 2n such that given positions have the opening bracket.
Note: position array is given in the form of (1-based indexing) [0, 1, 1, 0]. Here 1 denotes the positions at which open bracket should be placed. At positions with value 0, either of opening and closing bracket can be placed.
Input: n = 3, position = [0, 1, 0, 0, 0, 0]
The proper bracket sequences of length 6 and
opening bracket at position 2 are:
[ [ ] ] [ ]
[ [ [ ] ] ]
[ [ ] [ ] ]
Input: n = 2, position = [1, 0, 1, 0]
The only possibility is:
[ ] [ ]
Dynamic Programming approach of this problem has been already discussed here. In this post, recursive and recursion using memoization approach will be discussed.
- Mark all the positions with open brackets in the given array adj as 1.
- Run a recursive loop, such that –
- If count of total brackets(opening brackets subracted from closing brackets is less than zero), return 0.
- If the index reaches till n and if the total brackets=0, then a solution is obtained and return 1, otherwise return 0.
- If the index has 1 pre-assigned to it, return the function recursively with index+1 and increment the total brackets.
- Otherwise Return the function recursively by inserting open brackets at that index and incrementing total brackets by 1 + inserting closed brackets at that index and decrementing total brackets by 1 and move on to the next index till n.
Below is the Recursive solution for above algorithm:
Memoized Approach: Time complexity of the above algorithm can be optimized by using Memorization. The only thing to be done is to use an array to store the results of previous iterations so that there is no need to recursively call the same function more than once, if the value is already calculated.
Below is the required implementation:
# Python3 implemntation of above approach
# using memoizaion
N = 1000;
dp = [[-1 for x in range(N)]
for y in range(N)];
# Open brackets at postion 1
adj = [ 1, 0, 0, 0 ];
# Function to find Number of proper
# bracket expressions
def find(index, openbrk, n):
# If open-closed brackets<0 if (openbrk < 0): return 0; # If index reaches the end of expression if (index == n): # If brackets are balanced if (openbrk == 0): return 1; else: return 0; # If already stored in dp if (dp[index][openbrk] != -1): return dp[index][openbrk]; # If the current index has assigned # open bracket if (adj[index] == 1): # Move forward increasing the # length of open brackets dp[index][openbrk] = find(index + 1, openbrk + 1, n); else: # Move forward by inserting open as # well as closed brackets on that index dp[index][openbrk] = (find(index + 1, openbrk + 1, n) + find(index + 1, openbrk - 1, n)); # return the answer return dp[index][openbrk]; # Driver Code # DP array to precompute the answer n = 2; # Calling the find function to # calculate the answer print(find(0, 0, 2 * n)); # This code is contributed by mits [tabby title="C#"]
Time complexity: O(N2)
- Balanced expressions such that given positions have opening brackets
- Number of balanced bracket expressions that can be formed from a string
- Printing brackets in Matrix Chain Multiplication Problem
- Print all possible expressions that evaluate to a target
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Length of Longest Balanced Subsequence
- Check for balanced parenthesis without using stack
- Count Balanced Binary Trees of Height h
- Number of ways to partition a string into two balanced subsequences
- Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
- Generate all unique partitions of an integer | Set 2
- Queries for bitwise OR in the given matrix
- Queries for bitwise AND in the given matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.