educative.io

Question about space complexity

Shouldn’t the space complexity be O (N+K), instead of O(N)?

Hi @Caustic_Potash,

You are right, we do need to store K values as well, but K is a constant, so we can write it as O(N).

1 Like

You can understand it like this.

What would be the maximum value of ‘K’? It cannot be more than ‘N’, right? Which means K = O(N)

So the space complexity will be O(N+K) => O(N+N) => O(2N) => O(N).

2 Likes

Thanks, this explanation was helpful!