Skip to main content

Big O for Memory

Big O can describe memory the same way it describes time: how much extra space does the algorithm need as the input grows?

We count only the extra space — the variables and arrays the algorithm creates — not the input itself. This extra space is called auxiliary space.

O(1) space: a fixed number of variables, no matter the input. O(n) space: something new is stored per input element, like a copy or an associative array of seen values.


O(1) Space

An algorithm that keeps only a few plain variables uses constant space. The array can hold ten elements or ten million — the algorithm itself still owns just its counters.

Summing, finding the maximum, and linear search are all O(n) time but O(1) space: they read a lot and remember almost nothing.

Sum with one variable

The whole algorithm remembers exactly one number, $sum. Its memory use does not grow with the array: O(1) space.

Prompt
Write a PHP script that sums the array 3, 1, 4, 1, 5, 9, 2, 6 using a single extra variable and prints the sum, to show an O(n) time but O(1) space algorithm.
Output: PHP script
Sum with one variable

O(n) Space

As soon as the algorithm builds something with one entry per input element — a new array, an associative array — its space is O(n): double the input, double the memory.

That is not bad by itself. It is simply a cost you should notice, because it can be traded.

Build a new array

The output array gets one element for every input element. 5 in, 5 out — memory grows in step with the input: O(n) space.

Prompt
Write a PHP script that builds a new array with every element of the array 1, 2, 3, 4, 5 doubled, appending to an empty array, then prints the input array and the output array.
Output: PHP script
Build a new array

Trading Space for Time

Often you can spend memory to save time. The classic example is Two Sum: find two numbers in an array that add up to a target.

The direct way checks every pair — O(n²) time, O(1) space. The fast way walks the array once and remembers every number it has seen as a key in an associative array. For each number it asks: "have I already seen target - n?" — O(n) time, O(n) space.

This trade is usually worth it: memory can be added to a machine, time cannot.

Two Sum with two loops

Every pair is checked until a match appears — up to 15 checks for 6 elements, and the count grows with the square of the input. No extra memory is used.

Prompt
Write a PHP script that finds two numbers adding up to 9 in the array 2, 11, 15, 1, 8, 7 by checking every pair with two nested loops, counting the checks. Print every match with the number of checks so far, and at the end print the total number of checks.
Output: PHP script
Two Sum with two loops

Two Sum with an associative array

One pass, 6 checks. Each number is looked up with isset() and stored in the $seen associative array — constant-time work per element. The array is the O(n) memory that buys the O(n) time.

Prompt
Write a PHP script that finds two numbers adding up to 9 in the array 2, 11, 15, 1, 8, 7 in one pass: keep an associative array of seen numbers, and for each number check with isset whether target minus the number was already seen. Print every match, and at the end print that each element was checked once.
Output: PHP script
Two Sum with an associative array