-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipleLeftRotation.java
More file actions
57 lines (48 loc) · 1.54 KB
/
Copy pathMultipleLeftRotation.java
File metadata and controls
57 lines (48 loc) · 1.54 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 array;
import util.Util;
public class MultipleLeftRotation {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
int[] b = {2, 3};
int[][] ans = multipleRotation(a, b);
for (int[] e : ans) {
Util.printArray(e);
System.out.println();
}
}
static int[][] multipleRotation(int a[], int b[]) {
int n = a.length;
int m = b.length;
int[][] ans = new int[m][n];
int[] temp = new int[2 * n];
// populating temp with {1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }
for (int i = 0; i < n; i++) {
temp[i] = a[i];
temp[i + n] = a[i];
}
for (int i = 0; i < m; i++) {
/*
case when b = {1, 9}
if we have to rotate the array 9 times and the size of array is 5 then, it basically means:
9 mod b.length = 4, so start from 4th index.
Tips: rotating array 10 times mean completing the round 2 times as the size of array is 5.
*/
int offSet = (b[i]) % n;
for (int j = 0; j < n; j++) {
ans[i][j] = temp[j + offSet];
}
}
return ans;
}
static void rotateByk(int a[], int k, int[][] ans, int times) {
int n = a.length - 1;
for (int i = 0; i < k; i++) {
int temp = a[0];
for (int j = 0; j < a.length - 1; j++) {
a[j] = a[j + 1];
}
a[n] = temp;
ans[times] = a;
}
}
}