#C1004. Shift Right Array
Shift Right Array
Shift Right Array
You are given an array of integers and a non-negative integer \( k \). Your task is to shift the array to the right by \( k \) positions in a cyclic manner. In other words, every element is moved to the right by \( k \) steps, and the elements that fall off the end of the array reappear at the beginning.
If the length of the array is \( n \), then shifting by \( k \) positions is equivalent to shifting by \( k \mod n \) positions. If \( k = 0 \) or the array is empty, the array should remain unchanged.
Constraints: You must perform the shifting in-place without utilizing extra arrays. You may not use built-in functions for array manipulation.
Example:
Input: n = 5, array = [1, 2, 3, 4, 5], k = 2 Output: [4, 5, 1, 2, 3]
inputFormat
The input is provided via stdin and consists of three parts:
- An integer \( n \) representing the number of elements in the array.
- A line containing \( n \) space-separated integers which are the elements of the array. (If \( n = 0 \), this line may be empty.)
- An integer \( k \) representing the number of right shifts to perform.
For example:
5 1 2 3 4 5 2
outputFormat
Output the shifted array via stdout as a single line of space-separated integers. If the array is empty, output nothing.
For example:
4 5 1 2 3## sample
5
1 2 3 4 5
2
4 5 1 2 3