Grokking Advanced Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit (medium)
Table of Contents

Problem Statement

Examples

Try it yourself

Problem Statement

Given an integer array nums and an integer limit, return the maximum length of a continuous subarray such that the absolute difference between any two elements in this subarray is less than or equal to limit. The subarray must be non-empty.

Examples

Example 1:

  • Input: nums = [1, 3, 6, 7, 9, 10], limit = 3
  • Output: 3
  • Explanation: Consider the [6, 7, 9] or [7, 9, 10] array. The absolute difference between any two elements in these subarrays is at most 3.

Example 2:

  • Input: nums = [8, 2, 4, 10, 12], limit = 6
  • Output: 3
  • Explanation: The longest subarray is [8, 2, 4]. The absolute difference between any two elements in this subarray is at most 6.

Example 3:

  • Input: nums = [1, 5, 9, 13, 14], limit = 4
  • Output: 2
  • Explanation: Consider the [1, 5], [5, 9], [9, 13] or [13, 14] array. The absolute difference between any two elements in these subarrays is at most 4.

Constraints:

  • 1 <= nums.length <= 10<sup>5</sup>
  • 1 <= nums[i] <= 10<sup>9</sup>
  • 0 <= limit <= 10<sup>9</sup>

Try it yourself

Try solving this question here:

Python3
Python3

. . . .
Mark as Completed

Table of Contents

Problem Statement

Examples

Try it yourself