Skip to content

Commit f5d7715

Browse files
author
Rohit Dandnayak
committed
array-rotation & nge
1 parent f245d5f commit f5d7715

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/data-structures/array/array-rotation.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,35 @@ function rotateArray(arr) {
88
return arr;
99
}
1010

11+
function gcd(a, b) {
12+
if (a <= 0) return b;
13+
let x = b - a;
14+
return gcd(x, a);
15+
}
16+
17+
function rotateJuggling(arr, d) {
18+
let len = arr.length;
19+
for (let i = 0; i < gcd(d, len); i++) {
20+
let temp = arr[i];
21+
let j = i;
22+
while (1) {
23+
let k = j + d;
24+
if (k >= len) k = k - len;
25+
if (k === i) break;
26+
arr[j] = arr[k];
27+
j = k;
28+
}
29+
arr[j] = temp;
30+
}
31+
return arr;
32+
}
33+
1134
function rotateArrayBy(arr, d) {
1235
for (let i = 0; i < d; i++) {
1336
rotateArray(arr);
1437
}
1538
return arr;
1639
}
1740

18-
console.log(rotLeft([1, 2, 3, 4, 5], 3));
41+
console.log(rotateArrayBy([1, 2, 3, 4, 5], 3));
42+
console.log(rotateJuggling([1, 2, 3, 4, 5], 3));

src/data-structures/stack/nge.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function printNGE(arr) {
2+
for (let i, len = arr.length; i < len; i++) {}
3+
}

0 commit comments

Comments
 (0)