Back to DSA sheet
Rotate Array
MediumArraysRotate the array to the right by k positions, in place. Elements shifted off the end wrap around to the front.
Input format (stdin): the first line has n and k. The second line has n integers. Output the rotated array, space separated.
Examples
Input: 7 3
1 2 3 4 5 6 7
Output: 5 6 7 1 2 3 4
Each element moves three places to the right, wrapping around.
Input: 4 2
-1 -100 3 99
Output: 3 99 -1 -100
Constraints
- 1 <= n <= 10^5
- 0 <= k <= 10^5
Sheets
Love Babbar 450NeetCode 250Striver A2Z
rotate-array.cpp3 sample tests
Loading editor
Test results
Run the sample tests to check your solution against expected output.
Custom input (stdin)
Output
Run your code to see its output.