LogiQuest

374. Reverse Nodes in K-Groups

HardLinked List

Reverse the nodes of the list k at a time; a final group with fewer than k nodes stays as it is. Print the list.

Input format

Line 1: n k Line 2: n values

Output format

The modified list.

Example 1

Input

5 2
1 2 3 4 5

Output

2 1 4 3 5

Example 2

Input

5 3
1 2 3 4 5

Output

3 2 1 4 5

Constraints

1 ≤ k ≤ n ≤ 10⁵

Read input from standard input and print the answer to standard output. Your code is judged against 4 test cases, most of them hidden.

Loading the MyLoginn IDE…