-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRotateInArray.java
More file actions
46 lines (39 loc) · 981 Bytes
/
RotateInArray.java
File metadata and controls
46 lines (39 loc) · 981 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
44
45
46
package lecture4;
import java.util.Scanner;
public class RotateInArray {
static Scanner a= new Scanner(System.in);
public static void main(String[] args) {
int[] arr= takeinput();
int b=arr.length;
int[] newarr = new int[b];
int rot= a.nextInt();
rotate(arr,newarr , rot );
display(newarr);
}
public static int[] takeinput() {
System.out.println("size?");
int n = a.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = a.nextInt();
}
return arr;
}
public static void display(int[] a) {
for (int val : a) {
System.out.println(val);
}
}
public static void rotate(int[]arr , int[] newarr , int rot) {
rot = rot%arr.length;
if(rot<0)
rot = rot + arr.length;
int i = 0;
for(i=0 ; i<arr.length ; i++) {
if(i<rot)
newarr[i] =arr[i +arr.length - rot];
else
newarr[i] =arr[i - rot];
}
}
}