-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathArrayRotation.java
50 lines (48 loc) · 1.44 KB
/
ArrayRotation.java
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
49
50
import java.util.*;
class Main {
void rightRotate(int[] arr, int n) {
int temp;
for (int i = 1; i <= n; i++) {
temp = arr[arr.length - 1];
for (int j = arr.length - 1; j > 0; j--) {
arr[j] = arr[j - 1];
}
arr[0] = temp;
}
System.out.println("Input Array After Right Rotation By " + n +
" Positions :");
System.out.println(Arrays.toString(arr));
}
void leftRotate(int[] arr, int n) {
int temp;
for (int i = 0; i < n; i++) {
temp = arr[0];
for (int j = 0; j < arr.length - 1; j++) {
arr[j] = arr[j + 1];
}
arr[arr.length - 1] = temp;
}
System.out.println("Input Array After Left Rotation By " + n +
" Positions :");
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the length of the array you wish to rotate: ");
int l = s.nextInt();
int arr1[] = new int[l];
int arr2[] = new int[l];
System.out.println("Enter the Elements");
for (int i = 0; i < l; i++) {
arr1[i] = s.nextInt();
arr2[i] = arr1[i];
}
System.out.println("Enter the index of rotation");
int r = s.nextInt();
System.out.println("Input Array Before Rotation :");
System.out.println(Arrays.toString(arr1));
Main m = new Main();
m.rightRotate(arr1, r);
m.leftRotate(arr2, r);
}
}