Search code examples
algorithmbig-ospace-complexity

How is this space complexity calculated in this series sum?


Can someone explain the following space complexity calculation to me?

Given a stream of numbers of size b bits, calculate the sum of these numbers.

If we have seen T numbers so far, the sum is at most T2^b and hence needs at most O(b+log T) space.

Now, T2^b must be an upper bound because a more accurate upper bound would be T(2^b - 1).

But how did they calculate that the space upper bound is O(b +logT)?


Solution

  • With m bits, you can store numbers up to (about) 2m. So, working the other way, if you know the sum, you need to take a logarithm to get the number of bits (and hence the space complexity).

    Here, log(T 2b) = b + log(T).