- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArray.java
More file actions
Latest commit
57 lines (46 loc) · 1.05 KB
/
Copy pathArray.java
File metadata and controls
57 lines (46 loc) · 1.05 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
50
51
52
53
54
55
56
57
/*package whatever //do not write package name here */
importjava.io.*;
classGFG {
// Fuction to rotate array
staticvoidRotate(intarr[], intd, intn)
{
// Storing rotated version of array
inttemp[] = newint[n];
// Keepig track of the current index
// of temp[]
intk = 0;
// Storing the n - d elements of
// array arr[] to the front of temp[]
for (inti = d; i < n; i++) {
temp[k] = arr[i];
k++;
}
// Storing the first d elements of array arr[]
// into temp
for (inti = 0; i < d; i++) {
temp[k] = arr[i];
k++;
}
// Copying the elements of temp[] in arr[]
// to get the final rotated array
for (inti = 0; i < n; i++) {
arr[i] = temp[i];
}
}
// Function to print elements of array
staticvoidPrintTheArray(intarr[], intn)
{
for (inti = 0; i < n; i++) {
System.out.print(arr[i]+" ");
}
}
publicstaticvoidmain (String[] args) {
intarr[] = { 1, 2, 3, 4, 5, 6, 7 };
intN = arr.length;
intd = 2;
// Function calling
Rotate(arr, d, N);
PrintTheArray(arr, N);
}
}
// This code is contributed by ishankhandelwals.