-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftRotation.java
More file actions
49 lines (37 loc) · 1.28 KB
/
LeftRotation.java
File metadata and controls
49 lines (37 loc) · 1.28 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
48
49
package DSA;
import java.util.Scanner;
public class LeftRotation {
public static void main(String[] args) {
// LEFT ROTATING AN ARRAY BY N PLACES
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array : ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("\nEnter the array elements : ");
for (int i = 0; i < arr.length; i++) {
System.out.printf("Element %d : ", i);
arr[i] = sc.nextInt();
}
System.out.print("\nEnter number of place/s by which the array should be rotated : ");
int num = sc.nextInt();
int orgNum=num;
num = num % size;
reverse(arr, 0, num - 1);
reverse(arr, num, size - 1);
reverse(arr, 0, size - 1);
System.out.print("\nArray after " + orgNum + " left rotation/s : ");
for (int j : arr) {
System.out.print(j + " ");
}
sc.close();
}
public static void reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}