#C11052. Sliding Window Maximum Absolute Difference
Sliding Window Maximum Absolute Difference
Sliding Window Maximum Absolute Difference
You are given a sequence of n integers and an integer k representing the size of a sliding window. For each contiguous subarray (or window) of length k, compute the difference between the maximum and minimum values inside the window. In other words, for each window, you need to output:
$$\text{difference} = \max(window) - \min(window)$$
Note: The input guarantees that 1 ≤ k ≤ n.
The result should be a sequence of numbers separated by spaces corresponding to each sliding window.
inputFormat
The first line contains two integers n
and k
, where n
is the number of integers in the sequence and k
is the size of the sliding window.
The second line contains n
space-separated integers representing the sequence.
Example:
8 3 1 3 6 2 8 7 4 5
outputFormat
Output a single line containing n - k + 1
integers. Each integer is the maximum absolute difference (i.e. the difference between the maximum and minimum value) in the corresponding sliding window. The results should be separated by a single space.
Example:
5 4 6 6 4 3## sample
8 3
1 3 6 2 8 7 4 5
5 4 6 6 4 3