Chapter 7
These are the solutions to the exercises found in the section Exercises.
-
Here, N is the size of the array. Our loop runs for N / 2 times, as it processes two values each round of the loop. However, this is expressed as O(N) because we drop the constant.
-
It’s slightly tricky to define N in this case, since we’re dealing with two distinct arrays. The algorithm only processes each value once, so we could decide to call N the total number of values between both arrays, and the time complexity would be O(N). If we want to be more literal and call one array N and the other M, we could alternatively express the efficiency as O(N + M). However, because we’re simply adding N and M together, it’s simpler to just use N to refer to the total ...
Get A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 1 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.