Space complexity


why space complexity is O(1).


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


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


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).