You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/content/part02/array.asc
+45-1Lines changed: 45 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ endif::[]
7
7
=== Array
8
8
(((Array)))
9
9
(((Data Structures, Linear, Array)))
10
-
Arrays are one of the most used data structures. You probably have used it a lot but are you aware of the runtimes of `splice`, `shift`, `indexOf` and other operations? In this chapter, we are going deeper into the most common operations and their runtimes.
10
+
Arrays are one of the most used data structures. You probably have used it a lot but are you aware of the runtimes of `splice`, `shift`, `indexOf` and other operations? In this chapter, we are going deeper into the most common operations and their runtimes.
11
11
12
12
==== Array Basics
13
13
@@ -274,3 +274,47 @@ To sum up, the time complexity of an array is:
274
274
| splice ^| O(n) | Insert and remove from anywhere.
275
275
|===
276
276
//end::table
277
+
278
+
==== Array Exercises
279
+
280
+
1. Implement an efficient algorithm that rotate an array `a` an `k` number of times.
281
+
282
+
[source, javascript]
283
+
----
284
+
/**
285
+
* Rotate an array left by k number of times.
286
+
*
287
+
* @example
288
+
* rotateLeft([1,2,3], 1); // [2,3,1]
289
+
* rotateLeft([1,2,3,4,5], 4); // [5,1,2,3,4]
290
+
*
291
+
* rotateLeft(Array(1e6).fill(1), 1e4); //
292
+
*
293
+
* @param a - The array
294
+
* @param k - The number of times the array is rotated
295
+
*/
296
+
function rotateLeft(a, k) {
297
+
// write you code and test with examples
298
+
}
299
+
----
300
+
301
+
302
+
2. Implement an algorithm that takes two arrays of numbers and return a new array with the sum.
0 commit comments