<p>An array <code>nums</code> of length <code>n</code> is <strong>beautiful</strong> if:</p>

<ul>
	<li><code>nums</code> is a permutation of the integers in the range <code>[1, n]</code>.</li>
	<li>For every <code>0 &lt;= i &lt; j &lt; n</code>, there is no index <code>k</code> with <code>i &lt; k &lt; j</code> where <code>2 * nums[k] == nums[i] + nums[j]</code>.</li>
</ul>

<p>Given the integer <code>n</code>, return <em>any <strong>beautiful</strong> array </em><code>nums</code><em> of length </em><code>n</code>. There will be at least one valid answer for the given <code>n</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> n = 4
<strong>Output:</strong> [2,1,4,3]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> n = 5
<strong>Output:</strong> [3,1,2,5,4]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

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

class Solution {
public:
    vector<int>v, tmp;

    void partition( int l, int r){
        int idx = 0;
        for(int i = l; i <= r; i++ )
            if(v[i] % 2){
                tmp[idx] = (v[i] + 1) / 2;
                idx++;
            }
        for(int i = l; i <= r; i++ )
            if( (v[i] % 2) == 0){
                tmp[idx] = (v[i] + 1) / 2;
                idx++;
            }
        for(int i = l; i <= r; i++ ) v[i] = tmp[i - l];
    }

    void solve(int l, int r){
        if(l == r) return;
        partition(l, r);
        solve(l, (l + r) / 2 );
        for(int i = l; i <= (l + r) / 2; i++ ) v[i] = v[i] * 2 - 1;
        solve( (l + r) / 2 + 1, r);
        for(int i = (l + r) / 2 + 1; i <= r; i++ ) v[i] = v[i] * 2;
    }
    vector<int> beautifulArray(int n) {
       for(int i = 1; i <= n; i++ ) v.push_back(i);
       for(int i = 1; i <= n; i++) tmp.push_back(0);
       solve(0, n - 1);
       return v;
    }
};
8500