Below Algorithm is for sum of all elements in an array.
function sumofnumbers(a[],n)
{
sum=0
for(i=0 to n)
sum=sum+a[i]
print(sum)
}
Can anyone help me out with this Algorithm analysis. why the space complexity of above algorithm is O(n)? According to me this must be O(1). Because we are not considering any extra space over here. We are using given space to calculate sum of elements in array. for example in some sorting technique to sort elements we take extra array.
According to me
Formula to calculate Space complexity:
Space complexity = Input size + Auxillary space
Every algorithm contains it's required/own input size that is whatever space required by program to execute. So we have to consider only extra space(Auxillary space) required by program to execute that is other than Input size to calculate Space complexity.
So I applied this concept is it True??
And when to consider Both parameter(i.e Input size and Auxillary Space) to calculate Space complexity? because in some example like Linear Search i seen that some times they are not using Input size ?
Thank you!!