Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 490ee49

Browse files
feat(dsa): solve right rotation by 1 element
1 parent e39c0f4 commit 490ee49

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

‎Topic6/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
// Left rotation by 1 element
22
let arr1 = [1, 2, 3, 4, 5];
3-
let copy = arr1[0];
3+
let copy1 = arr1[0];
44
for (let i = 0; i < arr1.length - 1; i++) {
55
arr1[i] = arr1[i + 1];
66
}
7-
arr1[arr1.length - 1] = copy;
7+
arr1[arr1.length - 1] = copy1;
88
console.log(arr1);
9+
10+
// Right rotation by 1 element
11+
let arr2 = [1, 2, 3, 4, 5];
12+
13+
const copy2 = arr2[arr2.length - 1];
14+
for (let i = arr2.length - 1; i > 0; i--) {
15+
arr2[i] = arr2[i - 1];
16+
// 4 -> 5
17+
// 3 -> 4
18+
// 2 -> 3
19+
// 1 -> 2
20+
//
21+
}
22+
arr2[0] = copy2;
23+
console.log(arr2);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /