-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotateLeft.java
37 lines (37 loc) · 1.18 KB
/
rotateLeft.java
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
class rotateLeft
{
public static void main(String[] args)
{
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
//n determine the number of times an array should be rotated
int n = 3;
//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 left
for(int i = 0; i < n; i++)
{
int j, first;
//Stores the first element of the array
first = arr[0];
for(j = 0; j < arr.length-1; j++)
{
//Shift element of array by one
arr[j] = arr[j+1];
}
//First element of array will be added to the end
arr[j] = first;
}
System.out.println();
//Displays resulting array after rotation
System.out.println("Array after left rotation: ");
for(int i = 0; i< arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}