#C4763. Maximum Birds in Subarrays

    ID: 48337 Type: Default 1000ms 256MiB

Maximum Birds in Subarrays

Maximum Birds in Subarrays

You are given a series of datasets. In each dataset, you are given an integer n, a window size K, and an array of n integers representing the number of birds observed at consecutive time intervals. Your task is to compute, for each contiguous subarray of length K, the maximum number of birds observed.

The input consists of multiple datasets. Each dataset starts with a line containing two integers n and K. If both are 0, it indicates the end of input and should not be processed. For each dataset with n > 0, the next line will contain n integers separated by spaces. For each dataset, output a single line containing the maximum values for every subarray of size K in order, with a space between each value.

The maximum value for each subarray of length K is defined as:

\( \max\{a_i, a_{i+1}, \dots, a_{i+K-1}\} \) for \( 1 \leq i \leq n-K+1 \).

Ensure your solution reads from standard input (stdin) and writes to standard output (stdout).

inputFormat

The input consists of multiple datasets. For each dataset:

  1. A line with two integers n and K separated by a space. If both n and K are 0, the input terminates.
  2. If n > 0, a line with n integers separated by spaces representing the observations.

Example:

7 3
1 3 5 7 9 2 6
5 2
4 8 1 2 6
0 0

outputFormat

For each dataset (except the termination dataset), output a single line containing the maximum number of birds in each contiguous subarray of length K. The numbers should be space-separated.

Corresponding to the sample input, the output would be:

5 7 9 9 9
8 8 2 6
## sample
7 3
1 3 5 7 9 2 6
5 2
4 8 1 2 6
0 0
5 7 9 9 9

8 8 2 6

</p>