#K33022. Maximum Length Subarray with Limited Range
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
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
andd
, wheren
is the number of elements in the array (\(1 \le n\le 10^5\)) andd
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
.
6 3
1 3 6 4 1 2
3