<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p>

<p>Return <em>the max sliding window</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong> 
Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       <strong>3</strong>
 1 [3  -1  -3] 5  3  6  7       <strong>3</strong>
 1  3 [-1  -3  5] 3  6  7      <strong> 5</strong>
 1  3  -1 [-3  5  3] 6  7       <strong>5</strong>
 1  3  -1  -3 [5  3  6] 7       <strong>6</strong>
 1  3  -1  -3  5 [3  6  7]      <strong>7</strong>
</pre>

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

<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

// Enable aggressive compiler optimizations
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2,tune=native")

// Initialize the environment
bool init() {
    // Disable synchronization with C streams for faster I/O
    ios_base::sync_with_stdio(false);
    // Untie cout from cin to reduce flushes
    cout.tie(nullptr);
    return true;
}

// Call init once to set up the environment
bool _ = init();

class Solution {
public:
    // Function to find the maximum sliding window
    vector<int> maxSlidingWindow(vector<int>& nums, const int k) {
        // Create a fixed-size array to store the queue
        short q[nums.size()];
        // Initialize indices for the queue and result
        unsigned l = 0, r = 0;
        unsigned idx = 0;
        unsigned k_idx = 0;

        // Initialize the queue with the first k-1 elements
        while (idx < k - 1) {
            // Remove smaller elements from the end of the queue
            while (l < r && q[r - 1] < nums[idx]) {
                --r;
            }
            // Add the current element to the queue
            q[r++] = nums[idx++];
        }

        // Process the rest of the elements
        while (idx < nums.size()) {
            // Remove smaller elements from the end of the queue
            while (l < r && q[r - 1] < nums[idx]) {
                --r;
            }
            // Add the current element to the queue
            q[r++] = nums[idx];

            // Update the result
            if (q[l] == nums[k_idx]) {
                // Move to the next element in the queue
                ++l;
            } else {
                // Update the result with the maximum element
                nums[k_idx] = q[l];
            }

            // Move to the next element
            ++idx;
            ++k_idx;
        }

        // Resize the result vector to the exact size needed
        nums.resize(k_idx);

        // Return the result
        return std::move(nums);
    }
};
127646