------
## 最终结论
```python
from collections import deque

def longest_subarray(nums, limit):
    min_deque = deque()
    max_deque = deque()
    start = 0
    max_len = 0
    
    for end in range(len(nums)):
        while min_deque and nums[min_deque[-1]] >= nums[end]:
            min_deque.pop()
        min_deque.append(end)
        
        while max_deque and nums[max_deque[-1]] <= nums[end]:
            max_deque.pop()
        max_deque.append(end)
        
        while nums[max_deque[0]] - nums[min_deque[0]] > limit:
            start += 1
            if min_deque[0] < start:
                min_deque.popleft()
            if max_deque[0] < start:
                max_deque.popleft()
        
        max_len = max(max_len, end - start + 1)
    
    return max_len
```