#K8131. Longest Subarray with Limit

    ID: 35725 Type: Default 1000ms 256MiB

Longest Subarray with Limit

Longest Subarray with Limit

Given an array of integers and an integer limit, your task is to find the length of the longest contiguous subarray such that the difference between the maximum and minimum elements in that subarray is less than or equal to the limit. In other words, if S is a subarray, then S is valid if (\max(S) - \min(S) \le L), where L is the given limit. The subarray must be contiguous. Use efficient algorithms (e.g. the sliding window technique with deques) to handle large inputs.

inputFormat

The input is given in three lines via standard input (stdin):

  1. The first line contains a single integer (n) representing the number of elements in the array.
  2. The second line contains (n) space-separated integers representing the array elements.
  3. The third line contains an integer (L) representing the limit.

outputFormat

Output a single integer to standard output (stdout) indicating the length of the longest contiguous subarray that satisfies the condition (\max(S) - \min(S) \le L).## sample

7
8 2 4 7 10 6 5
5
4