-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathrotate.js
35 lines (29 loc) · 872 Bytes
/
rotate.js
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
// 1. Reversal approach
function rotate(nums, n) {
const length = nums.length;
n %= length;
reversal(nums, 0, length-1);
reversal(nums, 0, n-1);
reversal(nums, n, length-1);
}
function reversal(nums, start, end) {
while(start < end) {
[nums[start], nums[end]] = [nums[end], nums[start]];
start++;
end--;
}
}
// 2. BrutForce using unshift() and pop() methods
function rotateBrutforce(nums, n) {
for(let i=0; i< n; i++) {
nums.unshift(nums.pop());
}
}
let rotate1 = [1, 2, 3, 4, 5, 6, 7];
console.log("Before rotate: ", rotate1.join(", "));
rotate(rotate1, 4);
console.log("After rotate: ", rotate1.join(", "));
let rotate2 = [1, 2, 3, 4, 5, 6, 7];
console.log("Before rotate: ", rotate2.join(", "));
rotateBrutforce(rotate2, 4);
console.log("After rotate: ", rotate2.join(", "));