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

&nbsp;
Example 1:

Input: nums = [8,2,4,7], limit = 4
Output: 2 
Explanation: 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.


Example 2:

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


Example 3:

Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 109
	0 &lt;= limit &lt;= 109


# max absolte diff within a subarray is subarray max substracted by subarray min
class Solution:
    def longestSubarray(self, nums: List[int], limit: int) -> int:
        q = collections.deque() # monotonic decreasing deque to compute subarray max, index of
        q2 = collections.deque() # monotonic increasing deque to compute subarray min, index of
        
        # sliding window
        res = left = 0
        for right in range(len(nums)):
            # pop monotocity-violating numbers from right end
            while q and nums[q[-1]] <= nums[right]:
                q.pop()
            q.append(right)
            
            # pop monotocity-violating numbers from right end
            while q2 and nums[q2[-1]] >= nums[right]:
                q2.pop()
            q2.append(right)
            
            # sliding window
            while left < right and q and q2 and nums[q[0]] - nums[q2[0]] > limit:
                # compress window from left pointer
                if q and q[0] == left:
                    q.popleft()
                
                # compress left pointer
                if q2 and q2[0] == left:
                    q2.popleft()
                    
                left += 1
            
            if nums[q[0]] - nums[q2[0]] <= limit:
                res = max(res, right - left + 1)
        
        return res
class Solution {
    
    public int longestSubarray(int[] nums, int limit) {
        
        Deque<Integer> increasing = new LinkedList<Integer>(); // To keep track of Max_value index
        Deque<Integer> decreasing = new LinkedList<Integer>(); // To keep track of Min_value index
        
        int i = 0 ;
        int j = 0 ;
        int max_length = 0 ;
        
        while(j < nums.length){
            
            while(!increasing.isEmpty() && nums[increasing.peekLast()] >= nums[j]){
                increasing.pollLast() ;
            }
            
            increasing.add(j);
            
            while(!decreasing.isEmpty() && nums[decreasing.peekLast()] <= nums[j]){
                decreasing.pollLast() ;
            }
            
            decreasing.add(j);
            
            int max_val = nums[decreasing.peekFirst()] ;
            int min_val = nums[increasing.peekFirst()] ;
            
            if(max_val-min_val <= limit){
                max_length = Math.max(max_length , j-i+1);
            }else{
                
                // If maximum absolute diff > limit , then remove from dequeue and increase i
                while(i<=j  && nums[decreasing.peekFirst()] - nums[increasing.peekFirst()] > limit ){
                    
                    if(!increasing.isEmpty() && increasing.peekFirst() == i){
                        increasing.pollFirst() ;
                    }
                
                    if(!decreasing.isEmpty() && decreasing.peekFirst() == i){
                        decreasing.pollFirst() ;
                    }
                
                    i++ ; 
                }                
                               
            }
            
            
            j++ ;
        }
        
        return max_length ;
    }
}
class Solution {
public:
    int longestSubarray(vector<int>& nums, int limit) {

        int max_ans = 0;
        map <int, int> mp;
        int j = 0;
        for(int i = 0 ; i < nums.size() ; i++) {

            mp[nums[i]] ++;

            while( mp.size() > 0 && abs(mp.rbegin()->first - mp.begin()->first) > limit)
            {

                if(mp[nums[j]] > 0) {
                    mp[nums[j]] --;
                }

                if(mp[nums[j]] == 0) {

                    mp.erase(nums[j]);
                }

                j++;
            }

            max_ans = max(max_ans, i - j + 1);
        }

        return max_ans;
    }
};
/**
 * @param {number[]} nums
 * @param {number} limit
 * @return {number}
 */
var longestSubarray = function(nums, limit) {
    const maxQueue = [];
    const minQueue = [];
    let start = 0;
    let res = 0;
    for(let end = 0; end < nums.length; end ++) {
        const num = nums[end];
        while(maxQueue.length > 0 && maxQueue[maxQueue.length - 1] < num) {
            maxQueue.pop();
        }

        while(minQueue.length > 0 && minQueue[minQueue.length - 1] > num) {
            minQueue.pop();
        }
        maxQueue.push(num);
        minQueue.push(num);
        if(maxQueue[0] - minQueue[0] > limit) {
            if(maxQueue[0] === nums[start]) {
                maxQueue.shift();
            }

            if(minQueue[0] === nums[start]) {
                minQueue.shift();
            }

            start +=1;
        }
        res = Math.max(res, end - start + 1);
    }

    return res;
};
Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit