#K33022. Maximum Length Subarray with Limited Range

    ID: 24995 Type: Default 1000ms 256MiB

Maximum Length Subarray with Limited Range

Maximum Length Subarray with Limited Range

You are given an array of integers and an integer d. Your task is to find the maximum length of a contiguous subarray such that the absolute difference between any two elements in the subarray is at most d. In other words, for any subarray A[l...r], it should hold that

max{A[l...r]}min{A[l...r]}d.\max\{A[l...r]\} - \min\{A[l...r]\} \le d.

This problem tests your ability to use sliding window techniques or other data structures to efficiently determine the largest subarray satisfying the given constraint.

inputFormat

The input consists of two lines:

  • The first line contains two integers n and d, where n is the number of elements in the array (\(1 \le n\le 10^5\)) and d is the maximum allowed absolute difference.
  • The second line contains n space-separated integers representing the array elements.

outputFormat

Output a single integer representing the maximum length of a contiguous subarray that satisfies the condition that the absolute difference between any two elements is at most d.

## sample
6 3
1 3 6 4 1 2
3