Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Longest Continuous Subarray (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Find the length of the longest contiguous subarray within an array of integers, where the absolute difference between any two elements in this subarray does not exceed a specified limit. The challenge lies in ensuring the subarray is continuous and the range (difference between the maximum and minimum element in the subarray) fits within the given threshold.

Examples

  1. Example 1:

    • Input: Array = [10, 1, 2, 4, 7], Limit = 5
    • Expected Output: 3
    • Justification: The longest subarray where the absolute difference between any two numbers is at most 5 is [1, 2, 4].
  2. Example 2:

    • Input: Array = [4, 8, 5, 1, 7, 9], Limit = 3
    • Expected Output: 2
    • Justification: One possible longest subarray is [7, 9] where the difference between 7 and 9 is within the limit of 3.
  3. Example 3:

    • Input: Array = [3, 3, 3, 3, 3], Limit = 0
    • Expected Output: 5
    • Justification: Since all elements are the same, the entire array is the longest subarray with differences of 0.

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

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible