-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateAnArray.java
More file actions
43 lines (29 loc) · 845 Bytes
/
Copy pathRotateAnArray.java
File metadata and controls
43 lines (29 loc) · 845 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Array rotation left to right */
class RotateAnArray {
public static void leftRotate(int arr[]) {
int temp = arr[0];
for (int i = 1; i < arr.length; i++) {
arr[i-1] = arr[i];
}
arr[arr.length-1] = temp;
}
static void rightRotate(int arr[]) {
int temp = arr[arr.length-1];
for (int i = arr.length-1; i >0 ; i--) {
arr[i] = arr[i-1];
}
arr[0] = temp;
}
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,7};
rightRotate(arr);
for ( int nums : arr) {
System.out.print(nums+ " ");
}
System.out.println();
// leftRotate(arr);
// for ( int nums : arr) {
// System.out.print(nums+ " ");
// }
}
}