1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit LeetCode Solution
In this guide, you will get 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit LeetCode Solution with the best time and space complexity. The solution to Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit solution in C++
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit solution in Java
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit solution in Python
- Additional Resources

Problem Statement of Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
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.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 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 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
0 <= limit <= 109
Complexity Analysis
- Time Complexity:
- Space Complexity:
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit LeetCode Solution in C++
class Solution {
public:
int longestSubarray(vector<int>& nums, int limit) {
int ans = 1;
deque<int> minQ;
deque<int> maxQ;
for (int l = 0, r = 0; r < nums.size(); ++r) {
while (!minQ.empty() && minQ.back() > nums[r])
minQ.pop_back();
minQ.push_back(nums[r]);
while (!maxQ.empty() && maxQ.back() < nums[r])
maxQ.pop_back();
maxQ.push_back(nums[r]);
while (maxQ.front() - minQ.front() > limit) {
if (minQ.front() == nums[l])
minQ.pop_front();
if (maxQ.front() == nums[l])
maxQ.pop_front();
++l;
}
ans = max(ans, r - l + 1);
}
return ans;
}
};
/* code provided by PROGIEZ */
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit LeetCode Solution in Java
N/A
// code provided by PROGIEZ
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.