Chapter 3
These are the solutions to the exercises found in the section Exercises.
-
This is O(1). We can consider N to be the year passed into the function. But no matter what the year is, the algorithm doesn’t vary in how many steps it takes.
-
This is O(N). For N elements in the array, the loop will run N times.
-
This is O(log N). In this case, N is the number numberOfGrains, which is passed into the function. The loop runs as long as placedGrains < numberOfGrains, but placedGrains starts at 1 and doubles each time the loop runs. If, for example, numberOfGrains was 256, we’d keep doubling the placedGrains nine times until we reach 256, meaning that our loop would run eight times for an N of 256. If numberOfGrains was 512, our loop would run nine ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access