-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathArrayRotation.java
More file actions
34 lines (25 loc) · 919 Bytes
/
Copy pathArrayRotation.java
File metadata and controls
34 lines (25 loc) · 919 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
class ArrayRotation {
public static void main(String[] args) {
// n determine the number of times an array should be rotated.
int n = 6;
//initializing array
int [] arr = {5,8,9,6,2,3,10};
//Rotate the given array by n times
for(int i = 0; i < n; i++){
int j, temp;
//Stores the last element of array in temp
temp = arr[arr.length-1];
for(j = arr.length-1; j > 0; j--){
//Shifting element of the array by one position
arr[j] = arr[j-1];
}
//storing temp in last of array
arr[0] = temp;
}
System.out.println();
//Displays array after rotation
for(int e: arr){
System.out.print(e);
}
}
}