#K59377. Maximum Difficulty in K Consecutive Steps
Maximum Difficulty in K Consecutive Steps
Maximum Difficulty in K Consecutive Steps
Given a sequence of N terrains with their corresponding difficulty levels and an integer K, your task is to determine the maximum difficulty encountered in any contiguous subsegment (subarray) of exactly K terrains. Formally, if the difficulty levels are given as (a_1, a_2, \dots, a_N), you need to compute
[
\max_{1 \leq i \leq N-K+1} \left( \max{a_i, a_{i+1}, \dots, a_{i+K-1}} \right).
]
For example, if (N=5), (K=2), and the terrains are [5, 1, 2, 6, 7], the answer is 7 because among all consecutive pairs the maximum values are 5, 2, 6, and 7, and the overall maximum is 7.
This problem requires you to implement a solution that processes input from standard input (stdin) and writes the result to standard output (stdout).
inputFormat
The input consists of two lines. The first line contains two space-separated integers (N) and (K) where (N) is the number of terrains and (K) is the length of the contiguous segment to consider. The second line contains (N) space-separated integers representing the difficulty levels of the terrains.
outputFormat
Output a single integer on a new line — the maximum difficulty encountered in any contiguous subsegment of exactly (K) terrains.## sample
10 3
10 20 30 40 50 60 70 80 90 100
100