A cargo train is modeled as a singly linked list of container weights, front to back. Reverse the order of containers within every consecutive block of k containers: the 1st block is containers 1..k, the 2nd block is containers k+1..2k, and so on. If the final block has FEWER than k containers, leave that trailing block's order unchanged. Print the resulting weights.
Line 1: two space-separated integers n and k — the number of containers and the block size.
Line 2: n space-separated integers — the container weights, front to back.
n space-separated integers: the weights after reversing every full block of k.
Example 1
Input
7 3 1 2 3 4 5 6 7
Expected
3 2 1 6 5 4 7
Explanation
Blocks [1,2,3] and [4,5,6] are full and reverse to [3,2,1] and [6,5,4]; the trailing block [7] has only 1 < 3 items and stays: 3 2 1 6 5 4 7.
Example 2
Input
5 2 10 20 30 40 50
Expected
20 10 40 30 50
Explanation
Blocks [10,20] and [30,40] reverse to [20,10] and [40,30]; trailing [50] stays: 20 10 40 30 50.
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 →