-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayRotation2.java
More file actions
57 lines (49 loc) · 1.53 KB
/
Copy pathArrayRotation2.java
File metadata and controls
57 lines (49 loc) · 1.53 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
49
50
51
52
53
54
55
56
57
import java.util.Scanner;
public class ArrayRotation2 {
static void display(int a[]){
for(int i=0; i<a.length; i++){
System.out.print(a[i] + " ");
}
System.out.println();
}
static void rotateArray(int a[]) {
Scanner sc = new Scanner(System.in);
int left=0, right= a.length-1;
int mid= (left+right)/2;
System.out.print("Enter no. of rotations for left side: ");
int rotate_left = sc.nextInt();
while (rotate_left > 0){
for (int i = left; i < mid; i++) {
int temp=a[i];
a[i] = a[mid];
a[mid]=temp;
}
rotate_left--;
}
System.out.print("Enter no. of rotations for right side: ");
int rotate_right = sc.nextInt();
while (rotate_right > 0){
for (int i = mid+1; i < right; i++) {
int temp=a[i];
a[i] = a[right];
a[right]=temp;
}
rotate_right--;
}
display(a);
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
//ARRAY SIZE AND ELEMENTS INPUT
System.out.print("Enter size of array: ");
int size= sc.nextInt();
int arr[]= new int[size];
System.out.print("Enter " + size + " numbers: ");
for(int i=0; i<size; i++){
arr[i]= sc.nextInt();
}
System.out.print("Original array: ");
display(arr);
rotateArray(arr);
}
}