<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p>

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

<pre>
<strong>Input:</strong> nums = [8,2,4,7], limit = 4
<strong>Output:</strong> 2 
<strong>Explanation:</strong> All subarrays are: 
[8] with maximum absolute diff |8-8| = 0 &lt;= 4.
[8,2] with maximum absolute diff |8-2| = 6 &gt; 4. 
[8,2,4] with maximum absolute diff |8-2| = 6 &gt; 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 &gt; 4.
[2] with maximum absolute diff |2-2| = 0 &lt;= 4.
[2,4] with maximum absolute diff |2-4| = 2 &lt;= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 &gt; 4.
[4] with maximum absolute diff |4-4| = 0 &lt;= 4.
[4,7] with maximum absolute diff |4-7| = 3 &lt;= 4.
[7] with maximum absolute diff |7-7| = 0 &lt;= 4. 
Therefore, the size of the longest subarray is 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [10,1,2,4,7,2], limit = 5
<strong>Output:</strong> 4 
<strong>Explanation:</strong> The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 &lt;= 5.
</pre>

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

<pre>
<strong>Input:</strong> nums = [4,2,2,2,4,4,2,2], limit = 0
<strong>Output:</strong> 3
</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>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= limit &lt;= 10<sup>9</sup></code></li>
</ul>

#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

class MonotonicQueue {
    deque<int> q;         // Normal queue
    deque<int> maxQ;      // Front is the largest
    deque<int> minQ;      // Front is the smallest


public:
    // When pushing the new value into the queue, keep it in order
    void push(int n) {
        // Push to normal queue
        q.push_back(n);

        // Maintain maxQ and minQ
        while (!maxQ.empty() && maxQ.back() < n) {
            maxQ.pop_back();
        }
        maxQ.push_back(n);

        while (!minQ.empty() && minQ.back() > n) {
            minQ.pop_back();
        }
        minQ.push_back(n);
    }

    // Max is the head of the dequeue
    int max() {
        return maxQ.front();
    }

    int min() {
        return minQ.front();
    }

    int size() {
        return q.size();
    }

    // Pop removes the front value
    void pop(int n) {
        if (!q.empty()) {
            q.pop_front();

            if (n == maxQ.front()) {
                maxQ.pop_front();
            }

            if (n == minQ.front()) {
                minQ.pop_front();
            }
        }
    }
};

class Solution {
public:
    int longestSubarray(vector<int>& nums, int limit) {
        // Sliding window approach

        MonotonicQueue mq;
        int absDiff = 0;
        int maxLength = 0;

        // Sliding window pointers
        int left = 0, right = 0;

        while (right < nums.size()) {

            // Do Something
            mq.push(nums[right]);
            absDiff = abs(mq.max() - mq.min());

            // Update max length only if absDiff within limit
            if (absDiff <= limit) maxLength = max(maxLength, mq.size());

            // printDiagnostics(mq, absDiff, mq.size(), maxLength);

            // Expand window
            right++;

            // Window needs shrink
            while (absDiff > limit) {

                cout << "shrink" << endl;

                // Remove element from heaps
                mq.pop(nums[left]);
                absDiff = abs(mq.max() - mq.min());
                // printDiagnostics(mq, absDiff, mq.size(), maxLength);
                left++;
            }
        }

        return maxLength;      
    }
private:
    void printDiagnostics(MonotonicQueue &mq,
                          int absDiff,
                          int length,
                          int maxLength) {
            cout << "MQ Max: " << mq.max() << "/";
            cout << "MQ Min: " << mq.min() << "/";
            cout << "absDiff: " << absDiff << "/";
            cout << "Length: " << mq.size() << "/";
            cout << "Max Length: " << maxLength << endl;
            cout << "---------------" << endl;
    }
};
80170