<p>Given an integer <code>n</code>, find a sequence that satisfies all of the following:</p>

<ul>
	<li>The integer <code>1</code> occurs once in the sequence.</li>
	<li>Each integer between <code>2</code> and <code>n</code> occurs twice in the sequence.</li>
	<li>For every integer <code>i</code> between <code>2</code> and <code>n</code>, the <strong>distance</strong> between the two occurrences of <code>i</code> is exactly <code>i</code>.</li>
</ul>

<p>The <strong>distance</strong> between two numbers on the sequence, <code>a[i]</code> and <code>a[j]</code>, is the absolute difference of their indices, <code>|j - i|</code>.</p>

<p>Return <em>the <strong>lexicographically largest</strong> sequence</em><em>. It is guaranteed that under the given constraints, there is always a solution. </em></p>

<p>A sequence <code>a</code> is lexicographically larger than a sequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, sequence <code>a</code> has a number greater than the corresponding number in <code>b</code>. For example, <code>[0,1,9,0]</code> is lexicographically larger than <code>[0,1,5,6]</code> because the first position they differ is at the third number, and <code>9</code> is greater than <code>5</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> [3,1,2,3,2]
<strong>Explanation:</strong> [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 5
<strong>Output:</strong> [5,3,1,4,3,5,2,4,2]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 20</code></li>
</ul>

class Solution:
    def constructDistancedSequence(self, n: int) -> List[int]:

        output = []
        def backtrack(currIndex, bitMask, currList):
            nonlocal output
            if not output:
                currStart = currIndex
                while currStart <= m-1 and currList[currStart] != -1:
                    currStart += 1

                if currStart == m or bitMask == 0:
                    output = currList[:]

                currMask = bitMask
                curr_n = n
                while currMask:
                    if currMask & 1 == 1:
                        if curr_n == 1:
                            currList[currStart] = 1
                            backtrack(currStart, bitMask & (~(1<<(n-curr_n))), currList)
                            currList[currStart] = -1
                        elif currStart + curr_n <= m-1 and currList[currStart + curr_n] == -1:
                            currList[currStart] = curr_n
                            currList[currStart + curr_n] = curr_n
                            backtrack(currStart, bitMask & (~(1<<(n-curr_n))), currList)
                            currList[currStart] = -1
                            currList[currStart + curr_n] = -1

                    currMask >>= 1
                    curr_n -= 1
                        
        m = 2*n-1

        backtrack(0, (1<<n)-1 , [-1 for _ in range(m)])

        return output

        # m = 2*n-1
        # global_arr = []

        # def backtrack(curr_arr, curr_count, visited):
        #     if curr_count == n:
        #         nonlocal global_arr
        #         if not global_arr:
        #             global_arr = curr_arr[:]
        #         else:
        #             for j in range(m):
        #                 if global_arr[j] > curr_arr[j]:
        #                     break
        #                 elif global_arr[j] == curr_arr[j]:
        #                     continue
        #                 else:
        #                     global_arr = curr_arr[:]
        #                     break

        #         return

        #     for curr_num in range(n, 0, -1):
        #         if not visited[curr_num-1]:
        #             if curr_num > 1:
        #                 for j in range(m):
        #                     if curr_arr[j] == 0 and j+curr_num <= m-1 and curr_arr[j+curr_num] == 0:
        #                         curr_arr[j] = curr_num
        #                         curr_arr[j+curr_num] = curr_num
        #                         visited[curr_num-1] = True
        #                         backtrack(curr_arr, curr_count+1, visited)
        #                         visited[curr_num-1] = False
        #                         curr_arr[j] = 0
        #                         curr_arr[j+curr_num] = 0
        #             else:
        #                 for j in range(m):
        #                     if curr_arr[j] == 0:
        #                         curr_arr[j] = curr_num
        #                         visited[curr_num-1] = True
        #                         backtrack(curr_arr, curr_count+1, visited)
        #                         visited[curr_num-1] = False
        #                         curr_arr[j] = 0

        #     return

        
        # backtrack([0 for _ in range(m)], 0, [False for _ in range(n)])

        # return global_arr
16600