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

class Solution {
public:
    int longestSubarray(vector<int>& nums, int limit) {
        /*
         2,4,7,8
         2 last index which is less than equal to (2+limit)
         ar[i] last idx <= (limit + ar[i])
            i=j=0;
            mx-mi>=limit
        */

        int n = nums.size();

        int l,r;
        l=r=0;
        int mi,mx;
        mi=mx=nums[0];
        int longestSubarraySize = 1;
        
        int miIdx, mxIdx;
        miIdx = mxIdx;
        deque<pair<int,int>> windowMax;
        deque<pair<int,int>> windowMin;

        windowMax.push_back({0,mx});
        windowMin.push_back({0,mi});
        

        /*
        prefmins[idx] == prefmins[idx+1] , nums[idx+1] didn't change my mi
        else : nums[idx+1] < prefmins[idx]
        prefmaxs[idx] 
        minIdx1, maxIdx2: farthest indices from the right bound
        l = minIdx1 + 1 : min 

        7 4

        4,8,7 : [8,7] 
        */

        while(r<n)
        {
            // at point of entry mx-mi is <= limit.
            //
            while((mx-mi)<=limit && r<n)
            {
                longestSubarraySize = max(longestSubarraySize, (r-l+1));
                r++;
                if(r<n)
                {
                    // insert into windowMax.
                    //
                    while(windowMax.size()>0 && windowMax.back().second <= nums[r])
                    {
                        windowMax.pop_back();
                    }
                    windowMax.push_back({r,nums[r]});

                    // insert into windowMin.
                    //
                    while(windowMin.size()>0 && windowMin.back().second >= nums[r])
                    {
                        windowMin.pop_back();
                    }
                    windowMin.push_back({r,nums[r]});

                    mx = max(mx,nums[r]);
                    mi = min(mi,nums[r]);
                }
            }

            if(r==n) break;

            while((mx-mi)>limit && l<r)
            {
                l++;
                if(windowMin.size()>0 && windowMin.front().first<l)
                {
                    windowMin.pop_front();
                }
                mi = windowMin.front().second;
                if(windowMax.size()>0 && windowMax.front().first<l)
                {
                    windowMax.pop_front();
                }
                mx = windowMax.front().second;
            }
        }

        return longestSubarraySize;


    }
};
75191