-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateRight.java
More file actions
49 lines (37 loc) · 1.47 KB
/
RotateRight.java
File metadata and controls
49 lines (37 loc) · 1.47 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
package FML;
import java.util.Scanner;
class RotateRight {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input1 = sc.nextLine();
String list[] = input1.split(",");
//Initialize array
int [] arr = new int[list.length];;
//n determine the number of times an array should be rotated.
int n =sc.nextInt();;
//Displays original array
System.out.println("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
//Rotate the given array by n times toward right
for(int i = 0; i < n; i++){
arr[i] = Integer.parseInt(list[i]);
int j, last;
//Stores the last element of array
last = arr[arr.length-1];
for(j = arr.length-1; j > 0; j--){
//Shift element of array by one
arr[j] = arr[j-1];
}
//Last element of array will be added to the start of array.
arr[0] = last;
}
System.out.println();
//Displays resulting array after rotation
System.out.println("Array after right rotation: ");
for(int i = 0; i< arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}