Z22

Longest Subarray With Absolute Diff Less Than or Equal to Limit

Staff / Principal Engineer
Target Time: 40 - 50 Minutes
Type: NUMERIC

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) -> int

Input & Output Contract

Output Rule:Maximum subarray length.

System Constraints

1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9

Example 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:2
Example 2: [2, 4, 7, 2] fark 5
Input:[[10,1,2,4,7,2],5]
Expected Output:4
Example 3: limit=0
Input:[[4,2,2,2,4,4,2,2],0]
Expected Output:3
Loading Python Editor...
Input Parameters
[
  [
    8,
    2,
    4,
    7
  ],
  4
]
Expected Output
2