educative.io

Not understand the space complexity

Why the space complexity is O(1)? I think it should be O(n) because a list of n elements was created.

2 Likes

Hello @Jo11,

You’re correct in pointing out that the code creates a list of n elements; however, in the context of space complexity analysis, we don’t count the space used for the output/result storage towards the algorithm’s space complexity. Therefore, the space complexity of the code remains O(1).

If you have any further questions or feedback, please don’t hesitate to share. We’re here to assist you with your inquiries. Thank you.

Happy learning!

1 Like

however, in the context of space complexity analysis, we don’t count the space used for the output/result storage towards the algorithm’s space complexity.

Maybe you need to visit the provided solution again, The code is clearly using O(N) space, it uses res array to store intermediate calculations not just the final results.