A groundskeeper walks down a single row of hedge plants (given as a linked chain: each plant knows only the one right after it). Following a strict pruning pattern, the groundskeeper keeps the next m plants exactly as they are, then completely uproots the following d plants, and repeats this keep/uproot cycle down the row until reaching its end (the final uprooted run may contain fewer than d plants if the row runs out first, and the same is true of a final kept run). Given the row and the two cycle lengths, report the heights of the plants that remain, in their original left-to-right order.
Line 1: three integers n, m, d — the number of plants in the row, the number kept per cycle, and the number uprooted per cycle. Line 2: n integers height[0..n-1], the height of each plant in row order (the linked chain's node values).
Print the heights of the surviving plants, in their original order, separated by single spaces. Since m >= 1, the row always keeps at least its very first plant, so the output is never empty.
1 <= n <= 2*10^5 1 <= m <= 10^5 1 <= d <= 10^5 1 <= height[i] <= 10^9
Example 1
Input
8 2 3 1 2 3 4 5 6 7 8
Expected
1 2 6 7
Explanation
With m=2 kept and d=3 uprooted per cycle: keep plants at indices 0-1 (heights 1,2); uproot indices 2-4 (heights 3,4,5); keep indices 5-6 (heights 6,7); the row ends with only index 7 left, which falls in an uproot phase (height 8 is removed). Surviving heights in order: 1 2 6 7.
Example 2
Input
5 3 2 10 20 30 40 50
Expected
10 20 30
Explanation
With m=3 kept and d=2 uprooted: keep indices 0-2 (heights 10,20,30); uproot indices 3-4 (heights 40,50); the row ends. Surviving heights: 10 20 30.
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 →