Time is not the only cost. A program that needs more memory than the machine has will not run at all. This page shows how to count memory for iterative programs and, more carefully, for recursive ones.
Why memory matters
If an algorithm is slow, it still finishes, just later. If it needs more memory than is available, it fails (for example with a stack overflow or an allocation error). On the other hand, memory, unlike time, can be reused: when a function returns, its memory is freed for the next call.
It helps to split memory into two parts (Horowitz, Sahni and Mehta): a fixed part (code, simple variables, constants) that does not depend on n, and a variable part (arrays whose size depends on n, and the recursion stack).
Iterative programs
For a program without recursion, look at the declarations and allocations: how many variables, how long are the arrays, what does malloc request?
Summing an array of n numbers with a loop needs only i and sum: auxiliary space O(1).
Copying the array into a new array of size n needs O(n) auxiliary space.
An n×n table needs O(n2).
Recursive programs
Every active call needs a stack frame: space for its arguments, local variables and return address. So the space of a recursive algorithm is (space per frame) × (maximum recursion depth), the largest number of calls active at the same time. A function that calls itself once per level down to depth n (like a recursive factorial) uses O(n) space.
Binary recursion
A binary-recursive function may call itself twice. The study material’s example:
Binary recursion f(n)
1. If n equals 0 or 1, return 1.2. Recursively calculate f(n-1).3. Recursively calculate f(n-2).4. Return the sum of the results of steps 2 and 3.
The GCD algorithm
The study material’s GCD algorithm repeatedly subtracts the smaller number from the larger:
GCD by subtraction
while m > 0: if n > m: swap m and n m = m - nreturn n
Space in complexity theory
On a Turing machine (a theoretical computer with a tape for memory), the space complexity is the maximum number of tape cells used for any input of length n. The class PSPACE contains the decision problems that can be solved using a polynomial amount of memory, with no limit on time.
Key takeaways
Space complexity = peak memory in use, as a function of n; memory is reused after calls return.
Iterative programs: count variables and allocated arrays.
Recursive programs: space = frame size × maximum recursion depth.
Binary recursion f(n−1)+f(n−2): exponential time, Θ(n) space.
GCD by subtraction: O(1) space, between 1 and max(m,n) iterations.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
What is the space complexity of the Euclid (GCD) algorithm in the study material, and why? Is space complexity always more critical than time complexity?
Show worked answer
The space complexity is O(1): the algorithm stores only the integers m, n and a temporary t for swapping, and this does not depend on the values of m and n. Space can be critical because a program that needs more memory than the machine has cannot run at all, while a slow program still finishes eventually. But neither is always more critical: memory can be reused during execution, and in practice the right choice depends on the limits of the system (for example, a real-time system has strict time limits). Often we trade one for the other.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/analysis-of-algorithms/calculating-space-complexity/ to read this page.