-1

We receive a stream of $n$ integer numbers: $x_1, x_2,\dots, x_n$. Assume that each $x_i$ is a constant and can be stored with $O(1)$ bits.

Whenever a new number $x_i, i \geq 2$ is inputted, we need to evaluate and output the sum: $$ S_i := \sum_{j=1}^{i-1} |x_i - x_j|$$ After outputting it we receive $x_{i+1}$ etc.

My question is: can we do this using no more than $O(\log n)$ memory per input/output? A solution using $O(\text{polylog}(n))$ memory per iteration would also be good enough for me.

Observation: Let's say we would have defined: $$ S_i := \sum_{j=1}^{i-1} (x_i - x_j)^2$$ instead. Then it's clear we can compute each new $S_i$ using only $O(\log n)$ memory per input/output pair by always storing (and updating) the partial sums: $$s^1_{i} := \sum_{j=1}^{i-1} x_j, \ \ s^2_{i} := \sum_{j=1}^{i-1} x^2_j$$

and using the fact that: $$S_i = n x^2_i- 2x_i s^1_i + s^2_i$$

The motivation behind this post is that I am wondering whether this can be done for the sum of moduli too.

reservoir
  • 11
  • 3

1 Answers1

2

The function

$$\sum(x_i-x)^2$$ is quadratic in $x$ and can be represented by a constant number of parameters. It has $3$ degrees of freedom.

But the function

$$\sum|x_i-x|$$ is piecewise linear and requires the knowledge of all $x_i$'s. It has $n$ degrees of freedom.

So the answer is negative.