Skip to content

Commit 51d8180

Browse files
committed
✅ add solution for Left Rotation problem.
1 parent 5302649 commit 51d8180

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
| Subdomain | Challenge | Score | Difficulty | Solution |
3434
|:---------------------------:|:----------------------------------------------------------------------------------------------------------------------------:|:------:|:----------:|:-------------------------------------------------------------------------------------------------:|
35-
| Arrays | [Arrays - DS](https://www.hackerrank.com/challenges/arrays-ds) | 10 | Easy | [solution.js](arrays/arrays-ds.js) |
35+
| Arrays | [Arrays - DS](https://www.hackerrank.com/challenges/arrays-ds) | 10 | Easy | [solution.js](arrays/arrays-ds.js) |
36+
| Arrays | [Left Rotation](https://www.hackerrank.com/challenges/array-left-rotation) | 20 | Easy | [solution.js](arrays/left-rotation.js) |
3637

3738
## 👤 Author
3839

Diff for: arrays/left-rotation.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Problem: https://www.hackerrank.com/challenges/array-left-rotation/problem
2+
3+
function leftArray(d, arr) {
4+
let newArray = [];
5+
let tempArray = [];
6+
7+
if (d === arr.length) {
8+
return arr;
9+
}
10+
11+
for (var i = 0; i < arr.length; i++) {
12+
if (i + 1 <= d) {
13+
tempArray.push(arr[i]);
14+
} else {
15+
newArray.push(arr[i]);
16+
}
17+
}
18+
19+
return newArray.concat(tempArray);
20+
}
21+
22+
function leftArraySimple(d, arr) {
23+
if (d === arr.length) {
24+
return arr;
25+
}
26+
27+
const temp = arr.splice(0, d);
28+
29+
return [...arr, ...temp];
30+
}
31+
32+
// test 1
33+
let t1InputRotations = 2;
34+
let t1InputArray = [1, 2, 3, 4, 5];
35+
36+
let t1Result = leftArray(t1InputRotations, t1InputArray);
37+
console.log(t1Result);
38+
39+
// test 2
40+
let t2InputRotations = 2;
41+
let t2InputArray = [1, 2, 3, 4, 5];
42+
43+
let t2Result = leftArraySimple(t2InputRotations, t2InputArray);
44+
console.log(t2Result);

0 commit comments

Comments
 (0)