-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathArrayRotation.java
More file actions
29 lines (28 loc) · 867 Bytes
/
ArrayRotation.java
File metadata and controls
29 lines (28 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ArrayRotation {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
long n = Long.parseLong(br.readLine());
long[] arr = new long[(int) n];
String line = br.readLine();
String[] temp = line.trim().split("\\s+");
for (int i = 0; i < n; i++)
arr[i] = Long.parseLong(temp[i]);
int k = Integer.parseInt(br.readLine());
k = k % (int) n;
long[] rotated = new long[(int) n];
for (int i = 0, j = i + k; i < n; i++, j++) {
rotated[j] = arr[i];
if (j == n - 1)
j = -1;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < n; i++)
sb.append(rotated[i] + " ");
System.out.print(sb);
br.close();
}
}