Space complexity


#1

why space complexity is O(1).


#2

because,here space(memory) taken only by two variables ‘a’ and ‘b’. it doesn’t depend on the input size(N)


#3

More specifically O( c ), where c is a constant. It’s informally called O(1).


#4

Here the assignment of int a = 0, b = 0; happens only once and inside the loop a and b values are just been replaced so the space of the a and b remains the same as we are just replacing it. If we would have pushed into a list and as the list gorws with the loop then the space complexity would have been O(n).