5. All three-digit numbers from 100 to 999 are written in a row without spaces. Kostya underlined \( k \) consecutive digits in this sequence, and Andrey underlined other \( k \) consecutive digits in this sequence. It turned out that the \( k \)-digit numbers underlined by the boys are equal. For what largest \( k \) could this have happened?
Answer: $k=5$.

Solution. An example of a five-digit number that satisfies the condition of the problem is 22923. Indeed, Kostya could underline the fragment «22923» at the junction of the numbers 229 and 230, and Andrey - at the junction of the numbers 922 and 923.

Now we will show that it is not possible to choose a six-digit fragment. First, let's note two obvious facts.

1) If the middle digits of two consecutive three-digit numbers are different, then the smaller number ends in 9, and the larger one - in 0.
2) If the leading digits of two consecutive three-digit numbers are different, then the smaller number ends in 99, and the larger one - in 00.

Suppose the boys underlined a six-digit fragment «abcdef». It can be highlighted in the string in one of three ways, as shown in the diagram:

![](https://cdn.mathpix.com/cropped/2024_05_06_a7c052850cc589d6d026g-3.jpg?height=94&width=297&top_left_y=1021&top_left_x=888)

![](https://cdn.mathpix.com/cropped/2024_05_06_a7c052850cc589d6d026g-3.jpg?height=97&width=300&top_left_y=1019&top_left_x=1392)

(here $i, j, k$ are three-digit numbers, and asterisks denote some digits). Note that Kostya and Andrey used different methods, otherwise they would have underlined the same block. Suppose Kostya chose his block using the third method. Then $b$ and $e$ are the last digits of the consecutive numbers $k$ and $k+1$, so they are different. If Andrey used the second configuration, then the numbers $j+1$ and $j+2$ have different leading digits. By 2), the number $j+2$ ends in 00, and in particular, $f=0$. If Andrey used the first configuration, then the numbers $i$ and $i+1$ have different middle digits, and $f=0$ by 1). But $f \neq 0$, since the number $k+2$ is a three-digit number. The case where the boys used the first two configurations is handled similarly.