Z22
Longest Subarray With Absolute Diff Less Than or Equal to Limit
Problem Statement & Objective
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.
Function Signature (Python)
longest_subarray_abs_diff(nums: list[int], limit: int) -> intInput & Output Contract
Output Rule:Maximum subarray length.
System Constraints
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9Example Execution Walkthrough
nums = [8,2,4,7], limit = 4
result: 2 ([2, 4] or [4, 7])Example Test Scenarios (3)
Example 1
Input:
[[8,2,4,7],4]Expected Output:
2Example 2: [2, 4, 7, 2] fark 5
Input:
[[10,1,2,4,7,2],5]Expected Output:
4Example 3: limit=0
Input:
[[4,2,2,2,4,4,2,2],0]Expected Output:
3