#C10484. Height Differences in Musician Arrays

    ID: 39694 Type: Default 1000ms 256MiB

Height Differences in Musician Arrays

Height Differences in Musician Arrays

An orchestra conductor is interested in knowing the height differences among groups of musicians. Given an array of integers representing the heights of the musicians standing in a line, and a positive integer k, your task is to determine the difference between the tallest and shortest musician for every contiguous subarray of length k.

More formally, if the array of heights is \(H = [h_1, h_2, \dots, h_n]\), for every contiguous subarray \(H[i\ldots i+k-1]\) (where \(1 \leq i \leq n-k+1\)), compute the difference:

[ \text{difference}i = \max{h_i, h{i+1}, \dots, h_{i+k-1}} - \min{h_i, h_{i+1}, \dots, h_{i+k-1}} ]

The final output should be a sequence of these differences.

inputFormat

The input is given via stdin and consists of two lines. The first line contains two integers n and k, where n denotes the number of musicians and k is the length of the subarray. The second line contains n integers separated by spaces representing the heights of the musicians.

Constraints:

  • 1 \(\leq k \leq n\)
  • Heights are integers.

outputFormat

The output should be printed to stdout as a single line of space-separated integers. Each integer corresponds to the difference between the maximum and minimum heights in each subarray of length k.

## sample
7 3
1 3 6 4 10 3 8
5 3 6 7 7

</p>