<p>Given an array of integers <code>nums</code>, half of the integers in <code>nums</code> are <strong>odd</strong>, and the other half are <strong>even</strong>.</p>

<p>Sort the array so that whenever <code>nums[i]</code> is odd, <code>i</code> is <strong>odd</strong>, and whenever <code>nums[i]</code> is even, <code>i</code> is <strong>even</strong>.</p>

<p>Return <em>any answer array that satisfies this condition</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [4,2,5,7]
<strong>Output:</strong> [4,5,2,7]
<strong>Explanation:</strong> [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,3]
<strong>Output:</strong> [2,3]
</pre>

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

<ul>
	<li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>nums.length</code> is even.</li>
	<li>Half of the integers in <code>nums</code> are even.</li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow Up:</strong> Could you solve it in-place?</p>

#pragma GCC target("avx, mmx, sse2, sse3, sse4")

auto disableSync = [](void) noexcept -> int
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    return 0;
}();

class Solution final
{
public:
    std::vector<int> sortArrayByParityII(std::vector<int>& nums) const noexcept
    {
        const size_t nbElements = nums.size();
        std::queue<size_t> indices;
        std::mutex mtx;
        std::condition_variable cv;
        bool done = false;
        std::thread worker(
        [&](void)
            {
                for (size_t i = 0; i < nbElements; ++i)
                {
                    if (i % 2 && !(nums[i] % 2))
                    {
                        std::lock_guard<std::mutex> lock(mtx);
                        indices.push(i);
                        cv.notify_one();
                    }
                }
                {
                    std::lock_guard<std::mutex> lock(mtx);
                    done = true;
                    cv.notify_one();
                }
            });
        for (size_t i = 0; i < nbElements; ++i)
        {
            if (!(i % 2) && nums[i] % 2)
            {
                std::unique_lock<std::mutex> lock(mtx);
                cv.wait(lock, [&]() { return !indices.empty() || done; });

                if (!indices.empty())
                {
                    size_t j = indices.front();
                    indices.pop();
                    std::swap(nums[i], nums[j]);
                }
            }
        }
        worker.join();
        return nums;
    }
};
25300