-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotate.java
More file actions
32 lines (31 loc) · 1.01 KB
/
Rotate.java
File metadata and controls
32 lines (31 loc) · 1.01 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
/*Rotate a given array in anticlockwise direction by d elements*/
import java.util.Scanner;
import java.util.Arrays;
public class Rotate {
public static void main(String args[]) {
int[] arr;
int N, d, temp;
Scanner scan = new Scanner(System.in);
System.out.println("Enter length of array:");
N = scan.nextInt();
arr = new int[N];
System.out.println("Enter by how much array should rotate");
d = scan.nextInt();
System.out.println("enter array elements one by one");
for (int i=0;i<N;i++){
arr[i] = scan.nextInt();
}
System.out.println("entered array");
System.out.println(Arrays.toString(arr));
d = d%N;
for (int i=0;i<d;i++){
temp = arr[0];
for(int j=0;j<(N-1);j++){
arr[j] = arr[j+1];
}
arr[N-1] = temp;
}
System.out.println("Rotated Array");
System.out.println(Arrays.toString(arr));
}
}