-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleft-rotation.js
44 lines (33 loc) · 847 Bytes
/
left-rotation.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
36
37
38
39
40
41
42
43
44
// Problem: https://www.hackerrank.com/challenges/array-left-rotation/problem
function leftArray(d, arr) {
let newArray = [];
let tempArray = [];
if (d === arr.length) {
return arr;
}
for (var i = 0; i < arr.length; i++) {
if (i + 1 <= d) {
tempArray.push(arr[i]);
} else {
newArray.push(arr[i]);
}
}
return newArray.concat(tempArray);
}
function leftArraySimple(d, arr) {
if (d === arr.length) {
return arr;
}
const temp = arr.splice(0, d);
return [...arr, ...temp];
}
// test 1
let t1InputRotations = 2;
let t1InputArray = [1, 2, 3, 4, 5];
let t1Result = leftArray(t1InputRotations, t1InputArray);
console.log(t1Result);
// test 2
let t2InputRotations = 2;
let t2InputArray = [1, 2, 3, 4, 5];
let t2Result = leftArraySimple(t2InputRotations, t2InputArray);
console.log(t2Result);