<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>

#include <vector>

using namespace std;

class Solution {
public:
    vector<int> beautifulArray(int n) {
        vector<int> temp_vector(n);
        for (int i = 0; i < n; i++) {
            temp_vector[i] = i + 1;
        }

        return splitArray(temp_vector, n);
    }

    // Function to split the array
    vector<int> splitArray(const vector<int>& v, int n) {
        if (n == 1) {
            return vector<int>{1}; // Return vector with single element 1
        }

        vector<int> temp_v_evens(n / 2);
        vector<int> temp_v_odds(n - n / 2); // Adjust to handle all elements

        int j = 0;
        for (int i = 0; i < n; i++) {
            if (i < (n / 2 + n % 2)) {
                temp_v_odds[i] = v[i];
            } else {
                temp_v_evens[i - (n / 2 + n % 2)] = v[i];
            }
        }

        vector<int> odds = splitArray(temp_v_odds, (n / 2 + n % 2));
        vector<int> evens = splitArray(temp_v_evens, n / 2);

        // Manipulate arrays
        for (int i = 0; i < (n / 2 + n % 2); i++) {
            odds[i] = 2 * (odds[i] - 1) + 1; // Ensure the result is odd
        }
        for (int i = 0; i < (n / 2); i++) {
            evens[i] = 2 * evens[i]; // Double the even numbers
        }

        // Concatenate the arrays
        vector<int> result(n);
        j = 0;
        for (int i = 0; i < n; i++) {
            if (i < (n / 2 + n % 2)) {
                result[i] = odds[i];
            } else {
                result[i] = evens[j];
                j++;
            }
        }
        
        return result;
    }
};

16800