A geologist has logged the mineral-richness reading at each of n meters along a drill core; every reading is distinct (no two meters tie). For the expedition report, the geologist must submit one contiguous segment of exactly k consecutive meters as the representative "core sample." One segment is judged richer than another by comparing their readings meter by meter from the start of the segment: the first meter where the two segments differ decides which segment is richer, with the larger reading at that meter winning. Among all n - k + 1 possible contiguous segments of length k, output the richest one.
Line 1: two integers n and k — the number of logged meters and the required segment length. Line 2: n space-separated integers, the reading at each meter (all distinct).
A single line with the k readings of the richest segment, in order, space-separated.
1 <= k <= n <= 10^5 -10^9 <= reading <= 10^9 All n readings are distinct.
Example 1
Input
5 2 1 4 5 2 3
Expected
5 2
Explanation
The valid segment starts are meters 0 through 3 (since k=2), with readings 1, 4, 5, 2 at those starting meters. Meter 2 has the largest starting reading (5), so the richest segment begins there, giving the two-meter segment [5, 2].
Example 2
Input
4 4 7 2 9 1
Expected
7 2 9 1
Explanation
Since k equals n, there is only one possible segment — the entire core — which is trivially the richest (and only) choice: [7, 2, 9, 1].
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →