#K13111. Reverse Elements in Groups

    ID: 23841 Type: Default 1000ms 256MiB

Reverse Elements in Groups

Reverse Elements in Groups

Given an array of integers and an integer ( k ), reverse the elements of the array in groups of size ( k ). If the last group contains fewer than ( k ) elements, reverse those elements as well.

For example, if the array is [1, 2, 3, 4, 5, 6, 7, 8, 9] and ( k = 3 ), then after grouping and reversing each segment the result is [3, 2, 1, 6, 5, 4, 9, 8, 7].

inputFormat

Input is read from standard input (stdin). The first line contains two integers ( n ) and ( k ), where ( n ) is the number of elements in the array, and ( k ) is the group size. The second line contains ( n ) space-separated integers representing the array.

outputFormat

Output the transformed array to standard output (stdout) as a single line of space-separated integers.## sample

9 3
1 2 3 4 5 6 7 8 9
3 2 1 6 5 4 9 8 7