4. Let $n(n \geqslant 2)$ be a given integer. Find the smallest real number $\lambda$, such that for any real numbers $x_{1}, x_{2}, \cdots, x_{n} \in$ $[0,1]$, there exist $\varepsilon_{1}, \varepsilon_{2}, \cdots, \varepsilon_{n} \in\{0,1\}$, satisfying: for any $1 \leqslant i \leqslant j \leqslant n$, we have
$$
\left|\sum_{k=i}^{j}\left(\varepsilon_{k}-x_{k}\right)\right| \leqslant \lambda .
$$
(Zhang Duanyang)
4. The minimum value of $\lambda$ is $\frac{n}{n+1}$.

On one hand, take $x_{1}=x_{2}=\cdots=x_{n}=\frac{1}{n+1}$. For any $\varepsilon_{1}, \varepsilon_{2}, \cdots, \varepsilon_{n} \in\{0,1\}$, we will prove: there exist $1 \leqslant i \leqslant j \leqslant n$, such that
$$
\left|\sum_{k=i}^{j}\left(\varepsilon_{k}-x_{k}\right)\right| \geqslant \frac{n}{n+1} \text {. }
$$

In fact, when there exists some $1 \leqslant t \leqslant n$ such that $\varepsilon_{t}=1$, take $i=j=t$, then
$$
\begin{array}{l}
\left|\sum_{k=i}^{j}\left(\varepsilon_{k}-x_{k}\right)\right|=\left|\varepsilon_{t}-x_{t}\right| \\
=\left|1-\frac{1}{n+1}\right|=\frac{n}{n+1} .
\end{array}
$$

When for any $1 \leqslant t \leqslant n$ we have $\varepsilon_{t}=0$, take $i=1, j=n$, then
$$
\begin{array}{l}
\left|\sum_{k=i}^{j}\left(\varepsilon_{k}-x_{k}\right)\right|=\left|\sum_{k=1}^{n}\left(\varepsilon_{k}-x_{k}\right)\right| \\
=n\left|0-\frac{1}{n+1}\right|=\frac{n}{n+1} . \\
\text { Hence } \lambda \geqslant \frac{n}{n+1} .
\end{array}
$$

On the other hand, we show that when $\lambda=\frac{n}{n+1}$, for any real numbers $x_{1}, x_{2}, \cdots, x_{n} \in[0,1]$, there exist $\varepsilon_{1}, \varepsilon_{2}, \cdots, \varepsilon_{n} \in\{0,1\}$ that satisfy the requirement.
Let $S_{0}=0, S_{1}=x_{1}, S_{2}=x_{1}+x_{2}, \cdots$,
$$
S_{n}=x_{1}+x_{2}+\cdots+x_{n} \text {. }
$$

Divide the interval $[0,1)$ into $n+1$ subintervals
$$
\left[0, \frac{1}{n+1}\right),\left[\frac{1}{n+1}, \frac{2}{n+1}\right), \cdots,\left[\frac{n}{n+1}, 1\right),
$$

then there must be an interval that does not contain $\left\{S_{1}\right\},\left\{S_{2}\right\}, \cdots,\left\{S_{n}\right\}$, denote this interval as $\left[\alpha-\frac{1}{n+1}, \alpha\right)$, where, $\{x\}=x-$ $[x],[x]$ represents the greatest integer not exceeding the real number $x$.

Next, we provide the method to choose $\varepsilon_{1}, \varepsilon_{2}, \cdots, \varepsilon_{n}$: for any $1 \leqslant k \leqslant n$, if there exists a non-negative integer $m$, such that
$$
S_{k-1} \leqslant m+\alpha-\frac{1}{n+1}<m+\alpha \leqslant S_{k},
$$

then take $\varepsilon_{k}=1$; otherwise take $\varepsilon_{k}=0$.
Next, we verify that this method satisfies the requirement, i.e., for any $1 \leqslant i \leqslant j \leqslant n$, we have
$$
\left|\sum_{k=i}^{j}\left(\varepsilon_{k}-x_{k}\right)\right| \leqslant \frac{n}{n+1} \text {. }
$$

In fact, by the choice of $\alpha$, there exist non-negative integers $m, l$, such that
$$
\begin{array}{l}
m-1+\alpha \leqslant S_{i-1} \leqslant m+\alpha-\frac{1}{n+1}, \\
l-1+\alpha \leqslant S_{j} \leqslant l+\alpha-\frac{1}{n+1} .
\end{array}
$$

Then $x_{i}+\cdots+x_{j}=S_{j}-S_{i-1}$
$$
\in\left[l-m-\frac{n}{n+1}, l-m+\frac{n}{n+1}\right] \text {. }
$$

By the choice of $\varepsilon_{1}, \varepsilon_{2}, \cdots, \varepsilon_{n}$, we know that among $\varepsilon_{i}, \cdots, \varepsilon_{j}$ there are exactly $l-m$ ones.
$$
\begin{array}{l}
\text { Then } \varepsilon_{i}+\cdots+\varepsilon_{j}=l-m . \\
\text { Hence }\left(\varepsilon_{i}+\cdots+\varepsilon_{j}\right)-\left(x_{i}+\cdots+x_{j}\right) \\
\in\left[-\frac{n}{n+1}, \frac{n}{n+1}\right],
\end{array}
$$

i.e., $\left|\sum_{k=i}^{j}\left(\varepsilon_{k}-x_{k}\right)\right| \leqslant \frac{n}{n+1}$.
In summary, the minimum value of $\lambda$ is $\frac{n}{n+1}$.