-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRotateAnArray.java
More file actions
48 lines (39 loc) · 1.01 KB
/
Copy pathRotateAnArray.java
File metadata and controls
48 lines (39 loc) · 1.01 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.io.*;
import java.util.*;
public class RotateAnArray{
public static void display(int[] a){
StringBuilder sb = new StringBuilder();
for(int val: a){
sb.append(val + " ");
}
System.out.println(sb);
}
public static void rotate(int[] a, int k){
// write your code here
int n = a.length;
k %= n;
k = k < 0 ? k + n : k;
reverse(a, 0, n - k - 1);
reverse(a, n - k, n - 1);
reverse(a, 0, n - 1);
}
public static void reverse(int[] a, int l, int r) {
while(l < r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
l ++; r --;
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = Integer.parseInt(br.readLine());
}
int k = Integer.parseInt(br.readLine());
rotate(a, k);
display(a);
}
}