ArticlesCost

Cost

Space for time

Spend extra memory so a later lookup does not scan. The extra space is part of the cost, not a side effect.

Time is not the only cost. A hash map uses extra memory so that a later question, such as whether you have already seen the complement of this number, is a lookup instead of a scan. That is the space-for-time trade: O(n) extra memory, and the nested scan drops from quadratic to linear.

You will meet the same trade again. Memoisation stores answers so a recursive call does not recompute them. A sliding window stores a running total so the next slice does not start from zero. The memory is the reason the faster algorithm exists.

Say both numbers. "O(n) time and O(n) extra space" is a complete sentence. "O(n) time" on its own hides the array or the map you allocated. If the extra memory is a fixed handful of variables, say O(1) extra space and be glad of it.