Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to solve the computational problem.
sum(a,b)
return a+b
a and b is fixed part where it requires 4 bytes per variable
sum(x,n)
total =0
for(i=0: i <n; i++) {
total = total + x[i]
}
total, x and n are variables suppose n is large and we need to store 100 units.
It is the temporary space allocated by your algorithm to solve the problem.
Space complexity = Input Size + Auxillary Space.
sum(a,b)
return a+b
a and b is fixed part where it requires 4 bytes per variable
a+b will be used in Auxillary space
Time complexity is inversely proportional to the space complexity. So the time complexity can be lower if the space complexity is higher.
Fibonacci
5
3 2
2 1 0 1
1 1