January 2018
Intermediate to advanced
332 pages
7h 36m
English
A constant space algorithm is one in which the space consumed by an algorithm does not change by the size of the input or the algorithms input parameters in any way.
At this point, I would like to reiterate that when we talk about the space complexity of an algorithm we are talking about the auxiliary space that is consumed by the algorithm. This implies that even if our array is of size n, the auxiliary (or the extra) space that is consumed by our algorithm will remain constant, as shown in the following code snippet:
function firstElement(arr) { return arr[0];}
We can see here that the firstElement method does not take any more space, irrespective of what the input is. Hence, we can denote this as space complexity S(1)
Read now
Unlock full access