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 c9d9b0f

Browse files
Chore: Completed Activity-4 for Day-18
1 parent 7de5e5d commit c9d9b0f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

‎Day18/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,54 @@ console.log("Longest Substring: ", longestSubstring(str2));
159159
console.log("-------------------------------------------------");
160160
console.log("Activity 4: ");
161161

162+
// Task 8: Write a function to rotate an array by k positions. Log the rotated array.
163+
164+
const rotateArray = (arr, k) => {
165+
k = k % arr.length;
166+
let result = [];
167+
for (let i = 0; i < arr.length; i++) {
168+
result.push(arr[(i + k) % arr.length]);
169+
}
170+
return result;
171+
}
172+
173+
let arr6 = [4, 2, 9, 5, 1, 3, 6, 8, 7];
174+
let k = 3;
175+
console.log("Rotate Array: ", rotateArray(arr6, k));
176+
177+
// Task 9: Write a function to merge two sorted arrays into one sorted array. Log the merged array.
178+
179+
const mergeArrays = (arr1, arr2) => {
180+
let result = [];
181+
let i = 0;
182+
let j = 0;
183+
184+
while (i < arr1.length && j < arr2.length) {
185+
if (arr1[i] < arr2[j]) {
186+
result.push(arr1[i]);
187+
i++;
188+
} else {
189+
result.push(arr2[j]);
190+
j++;
191+
}
192+
}
193+
194+
while (i < arr1.length) {
195+
result.push(arr1[i]);
196+
i++;
197+
}
198+
199+
while (j < arr2.length) {
200+
result.push(arr2[j]);
201+
j++;
202+
}
203+
204+
return result;
205+
}
206+
207+
let arr7 = [1, 3, 5, 7];
208+
let arr8 = [2, 4, 6, 8];
209+
console.log("Merge Arrays: ", mergeArrays(arr7, arr8));
210+
162211
console.log("-------------------------------------------------");
163212
console.log("Activity 5: ");

0 commit comments

Comments
(0)

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