-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateArray.java
55 lines (46 loc) · 1.27 KB
/
RotateArray.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Given an array of n integers and a number, d, perform d left rotations on the array.
//Then print the updated array as a single line of space-separated integers.
import java.util.Scanner;
public class RotateArray
{
/*Function to left rotate arr[] of size n by d*/
void leftRotate(int arr[], int d, int n)
{
int i;
for (i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[i] = temp;
}
/* utility function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i] + " ");
}
// Driver program to test above functions
public static void main(String[] args)
{
RotateArray rotate = new RotateArray();
// System.out.println("Input Arr Size : ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// System.out.println("Enter value for d : ");
Scanner ins = new Scanner(System.in);
int d = ins.nextInt();
int arr[] = new int[n];
for(int arr_i=0; arr_i < n; arr_i++)
{
arr[arr_i] = in.nextInt();
}
rotate.leftRotate(arr, d, n);
rotate.printArray(arr, n);
}
}