<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 {
public:
// backtracking: try to start placing greater number first to ensure lexico condition
// you still might have to place a smaller number earlier, so keep a used num array
// res has to be 2xN-1 (1 is used once)
// dfs:
//   if indx == sz of res: all filled, return true
//   if res[indx] != -1: index visited before, go to next indx with dfs(indx+1)
//   loop (k) n to 1: [k num to be placed] 
//      if k is used: skip
//      if k is not 1 [used twice] and i+k is out of bounds or already filled: skip
//      mark k used
//      set res[indx] = k, if k is not 1: also set res[indx+k] = k
//      dfs(indx+1) -> return true if fxn returns true
//      [if not returned above attempt didn't work]
//      mark k unused
//      res[indx] = -1, if k is not 1 reset res[indx+k] as well
//  [if not returned in the loop, it didn't work] return false

    bool dfs(int i, int n, vector<bool> &used, vector<int> &res) {
        if(i == 2*n-1)
            return true;
        if(res[i] != -1)
            return dfs(i+1, n, used, res);
        for (int k = n; k > 0; k--) {
            if (used[k])
                continue;
            if (k != 1 &&  (i+k >= 2*n-1 || res[i+k] != -1))
                continue;
            used[k] = true;
            res[i] = k;
            if(k != 1)                 
                res[i + k]  = k; 
            if(dfs(i+1, n, used, res))
                return true;
            res[i] = -1;
            if (k != 1)
                res[i + k] = -1;
            used[k] = false;
        }
        return false;
    }

    vector<int> constructDistancedSequence(int n) {
        vector<int> res(2*n-1, -1);
        vector<bool> used(n+1,false);
        dfs(0, n, used, res);
        return res;
    }
};
8500