Problem 23
Rotate Array Right
Easy · 10 pts · Array
Problem
You are given an array of N integers and an integer K. Rotate the array to the right by K positions: every element moves K places to the right, and elements that fall off the end wrap around to the front. K can be much larger than N. Rotating by N positions brings the array back to where it started, so only K mod N matters. After that, the last K mod N elements move to the front, followed by the rest in their original order.
Input
First line: integers N and K. Second line: N space-separated integers.
Output
Print the rotated array, space-separated.
Constraints
1 <= N <= 100000; 0 <= K <= 1000000000; -1000000000 <= A[i] <= 1000000000
Example input
5 2 1 2 3 4 5
Example output
4 5 1 2 3
Explanation
Rotating 1 2 3 4 5 right by 2 moves the last two elements, 4 and 5, to the front, giving 4 5 1 2 3.