#K40277. Maximum Height Difference in Consecutive Buildings
Maximum Height Difference in Consecutive Buildings
Maximum Height Difference in Consecutive Buildings
You are given a sequence of N building heights and an integer K. Your task is to find the maximum difference between the tallest and the shortest building in any group of K consecutive buildings.
In other words, for every contiguous subsequence of K buildings, compute the difference using the formula:
$$diff = \max_{i \le j \le i+K-1} H_j - \min_{i \le j \le i+K-1} H_j,$$
and output the maximum such difference among all valid groups.
Note: Input is provided via standard input and the answer must be printed to standard output.
inputFormat
The first line contains two integers N and K, where N is the number of buildings and K is the size of the consecutive group.
The second line contains N space-separated integers representing the heights of the buildings.
For example:
5 3 1 5 9 3 8
outputFormat
Output a single integer, which is the maximum difference between the tallest and the shortest building found in any contiguous group of K buildings.
For the sample input above, the output should be:
8## sample
5 3
1 5 9 3 8
8
</p>