#C8316. Longest Contiguous Subarray with Bounded Difference
Longest Contiguous Subarray with Bounded Difference
Longest Contiguous Subarray with Bounded Difference
You are given a sequence of n integers A = {a0, a1, ..., an-1} along with an integer K. 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 K.
Formally, if subarray is defined as A[l...r] (0 ≤ l ≤ r < n), you need to maximize (r - l + 1) under the condition:
[ \max_{l \le i \le r} (a_i) - \min_{l \le i \le r} (a_i) \le K ]
Input: The first line contains two integers, n and K. The second line contains n space-separated integers representing the sequence A.
Output: Print a single integer, the length of the longest contiguous subarray satisfying the above condition.
Example:
Input: 6 1 1 3 2 4 1 2</p>Output: 2
inputFormat
The first line contains two integers n
and K
separated by a space.
The second line contains n
integers separated by spaces representing the array A
.
outputFormat
Output a single integer representing the length of the longest contiguous subarray where the difference between the maximum and minimum elements is less than or equal to K
.
6 1
1 3 2 4 1 2
2
</p>