TIME AND SPACE COMPLEXITY / ONLINE JUDGE › TIME AND SPACE COMPLEXITY
Space complexity describes how the memory use of an algorithm grows as the input size n grows. For the array [4, 2, 7, 1], the four slots holding those values are input space. They belong to the data given to the algorithm, not to the memory requested by the summing steps themselves.
Total space includes the input array and every extra value used while the algorithm runs. Auxiliary space reports only the extra working memory requested by the algorithm, excluding the input space. The array still consumes real memory, but a claim such as O(1) auxiliary space says that the algorithm adds only a fixed amount beyond the array.
The running array is [4, 2, 7, 1]. What kind of space do its four original slots represent?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The direct method reads each input slot and reuses two working variables: i for the current index and sum for the running total. It does not create a new stored value for each array element.
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}| I | VALUE READ | SUM AFTER THE VISIT |
|---|---|---|
| 0 | 4 | 4 |
| 1 | 2 | 6 |
| 2 | 7 | 13 |
| 3 | 1 | 14 |
For [4, 2, 7, 1], i visits indices 0 through 3 and sum changes from 0 to 4, 6, 13, and 14. If the input grows, the loop makes more visits, but it still needs only i and sum. The number of working variables stays fixed, so the direct method uses O(1) auxiliary space.
A copy-first method creates a separate buffer and places every input value into it before summing. For the running input, the buffer receives 4, then 2, then 7, then 1. It has four occupied slots before the final sum is computed.
int buffer[n];
for (int i = 0; i < n; i++) {
buffer[i] = arr[i];
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += buffer[i];
}The original array remains input space, while the buffer is auxiliary space. There is one buffer slot per input value, so an input of size n requires n extra slots. The fixed variables i and sum do not change that growth, making the copy-first method O(n) auxiliary space.
Classify the auxiliary space of the direct sum and the copy-first sum.
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Space is measured by the largest amount of storage that is live at the same time. You do not add memory for every read, assignment, or loop iteration. The same sum cell is reused after each read, so its repeated updates do not create repeated storage.
At the direct method's peak, the input array [4, 2, 7, 1] is live together with the fixed cells i and sum. Its auxiliary part stays fixed, even though the loop visits four slots and ends with sum equal to 14.
At the copy-first method's peak, the original input array, the four-slot buffer, and the fixed cells i and sum are live together. For a general input of size n, that snapshot contains the input space, n auxiliary buffer slots, and fixed working space. The growing part is the buffer, so the auxiliary cost is O(n).