#P1886. Sliding Window Minimum and Maximum
Sliding Window Minimum and Maximum
Sliding Window Minimum and Maximum
Given a sequence of length ( n ) and a window of size ( k ), slide the window one position at a time from left to right. For each window, compute the minimum and maximum values among the elements within the window.
For example, for the sequence ( a = [1, 3, -1, -3, 5, 3, 6, 7] ) and ( k = 3 ), the process is illustrated below:
$$\def\arraystretch{1.2}
\begin{array}{|c|c|c|}\hline
\textsf{Window} & \textsf{Minimum} & \textsf{Maximum} \ \hline
[1,; 3,; -1] & -1 & 3 \ \hline
[3,; -1,; -3] & -3 & 3 \ \hline
[-1,; -3,; 5] & -3 & 5 \ \hline
[-3,; 5,; 3] & -3 & 5 \ \hline
[5,; 3,; 6] & 3 & 6 \ \hline
[3,; 6,; 7] & 3 & 7 \ \hline
\end{array}$$
inputFormat
The first line contains two integers ( n ) and ( k ). The second line contains ( n ) space-separated integers representing the sequence.
outputFormat
For each sliding window, output a line containing two integers: the minimum and maximum values in that window.
sample
8 3
1 3 -1 -3 5 3 6 7
-1 3
-3 3
-3 5
-3 5
3 6
3 7
</p>