<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:
    def longestSubarray(self, nums: List[int], limit: int) -> int:
        n=len(nums)
        i=0
        ans=[]
        for j in range(n):
            bisect.insort(ans,nums[j])
            if ans[-1]-ans[0]>limit:
                ans.pop(bisect.bisect(ans,nums[i])-1)
                i+=1
        return j-i+1        

# class Solution:
#     def longestSubarray(self, nums: List[int], limit: int) -> int:
#         max_deque = deque()
#         min_deque = deque()
#         left = 0
#         max_length = 0

#         for right in range(len(nums)):
#             # Maintain the max_deque in decreasing order
#             while max_deque and max_deque[-1] < nums[right]:
#                 max_deque.pop()
#             max_deque.append(nums[right])

#             # Maintain the min_deque in increasing order
#             while min_deque and min_deque[-1] > nums[right]:
#                 min_deque.pop()
#             min_deque.append(nums[right])

#             # Check if the current window exceeds the limit
#             while max_deque[0] - min_deque[0] > limit:
#                 # Remove the elements that are out of the current window
#                 if max_deque[0] == nums[left]:
#                     max_deque.popleft()
#                 if min_deque[0] == nums[left]:
#                     min_deque.popleft()
#                 left += 1

#             max_length = max(max_length, right - left + 1)

#         return max_length

# class Solution:
#     def longestSubarray(self, nums: List[int], limit: int) -> int:
#         if len(nums) == 0:
#             return 0

#         max_len = 1
#         max_val = [nums[0]]
#         min_val = [nums[0]]
#         left = 0
#         for i in range(1, len(nums)):
#             while len(max_val)>0 and nums[i] > max_val[-1]:
#                 max_val.pop()
#             max_val.append(nums[i])

#             while len(min_val)>0 and nums[i] < min_val[-1]:
#                 min_val.pop()
#             min_val.append(nums[i])

#             print("Max", max_val)
#             print("Min", min_val)
#             # if nums[i] > max_val:
#             #     max_val = nums[i]
#             # elif nums[i] < min_val:
#             #     min_val = nums[i]

#             if max_val[0]-min_val[0] <= limit:
#                 max_len = max(max_len, i-left+1)
#             else:
#                 left += 1
#                 if nums[left-1] == max_val[0]:
#                     del max_val[0]
#                     # new_max = 0
#                     # for el in nums[left: i+1]:
#                     #     if el > new_max:
#                     #         new_max = el

#                     #     if el == max_val:
#                     #         break

#                     # max_val = new_max
#                 elif nums[left-1] == min_val[0]:
#                     del min_val[0]
#                     # new_min = 1e9
#                     # for el in nums[left: i+1]:
#                     #     if el < new_min:
#                     #         new_min = el

#                     #     if el == min_val:
#                     #         break

#                     # min_val = new_min

#         return max_len
1300