-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_array_by_k_position.js
More file actions
executable file
·39 lines (30 loc) · 1.03 KB
/
rotate_array_by_k_position.js
File metadata and controls
executable file
·39 lines (30 loc) · 1.03 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
// rotate array by k position
// pseudocode
// k = k % n // to handle k > n
// reverse(arr, 0, n - 1)
// reverse(arr, 0, k - 1)
// reverse(arr, k, n - 1)
// 1. reverse whole array
// 2. reverse first k elements
// 3. reverse remaining n-k elements
function rotateArrayByKPosition(arr, k) {
if(!Array.isArray(arr)) throw new TypeError("Input must be an array");
if(arr.length === 0) return arr;
k = k % arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
return arr;
}
function reverse(arr, start, end) {
while(start < end) {
[arr[start], arr[end]] = [arr[end], arr[start]];
start++;
end--;
}
}
console.log(rotateArrayByKPosition([1,2,3,4,5,6,7], 3)); // [5,6,7,1,2,3,4]
console.log(rotateArrayByKPosition([-1,-100,3,99], 2)); // [3,99,-1,-100]
console.log(rotateArrayByKPosition([1,2,3,4,5], 7)); // [4,5,1,2,3]
console.log(rotateArrayByKPosition([], 3)); // []
console.log(rotateArrayByKPosition([1], 10)); // [1]