August 2024
Intermediate to advanced
516 pages
11h 47m
English
These are the solutions to the exercises found in the section Exercises.
Here is the completed table:
N Elements | O(N) | O(log N) | O(N2) |
|---|---|---|---|
100 | 100 | About 7 | 10,000 |
2,000 | 2,000 | About 11 | 4,000,000 |
The array would have sixteen elements since 162 is 256. (Another way of saying this is that the square root of 256 is 16.)
The algorithm has a time complexity of O(N2). N, in this case, is the size of the array. We have an outer loop that iterates over the array N times, and for each of those times, an inner loop iterates over the same array N times. This results in N2 steps.
The following version is O(N), as we only iterate through the array once:
| | function greatestNumber(array) { |
| | if (array.length === 0) { return null; } |
| | |
| | let greatestNumberSoFar ... |
Read now
Unlock full access