-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraysLeftrotation.java
More file actions
39 lines (34 loc) · 1.2 KB
/
Copy pathArraysLeftrotation.java
File metadata and controls
39 lines (34 loc) · 1.2 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
import java.util.*;
import java.util.Scanner;
/* left rotation: rotation time t=2
before rotation: a=[1,2,3,4,5]
after rotation: a'=[3,4,5,1,2]
*/
public class ArraysLeftrotation {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int size;
int i, RotateTime;
System.out.print("Enter the size of the array:");
size = s.nextInt();
int a[] = new int[size]; // original array:
int RotateArray[] = new int[size]; // Rotatearray
System.out.print("\nEnter the array elements:");
for (i = 0; i < size; i++) {
a[i] = s.nextInt();
}
System.out.print("\nThe array element are:");
for (i = 0; i < size; i++) {
System.out.print(a[i] + " ");
}
System.out.print("\nEnter the number of Left Rotation:");
RotateTime = s.nextInt();
// rotation logic:
for (i = 0; i < size; i++) {
int position = (i + size - RotateTime) % size;
// assigning rotate arrays:
RotateArray[position] = a[i];
}
System.out.print("Array after " + RotateTime + " left rotation: " + Arrays.toString(RotateArray));
}
}