A balanced bracket sequence is a string made only of the characters ( and ) in which every opening bracket has a matching closing bracket, and every closing bracket has a matching opening bracket. For example, (())() is a balanced bracket sequence, while (())(() and ())(() are not.
Let A and B be two bracket sequences of the same length. We say that A is lexicographically smaller than B, written A<B, when both of the following hold:
- A and B differ in at least one position.
- At the leftmost position where A and B differ, A has a
( and B has a ).
For example, (())()<()()(), because the two strings first differ at the second position from the left, where the first string has a ( and the second has a ).
For a fixed length N, the relation < orders all balanced bracket sequences of length N. For N=6 the order is:
((()))
(()())
(())()
()(())
()()()
Given N and a positive integer M, find the M-th balanced bracket sequence of length N in this order.