Ondra, Matěj, and Kuba are returning from gathering nuts, and they have a total of 120. Matěj complains that Ondra, as always, has the most. Their father orders Ondra to give Matěj enough nuts to double his amount. Now Kuba complains that Matěj has the most. On their father's command, Matěj gives Kuba enough nuts to double his amount. Then Ondra gets upset, saying he has the least now. Kuba thus gives Ondra enough nuts to double his amount. Now they all have the same amount, and finally, there is peace.

How many nuts did each of the boys originally have?

(M. Volfová)

Hint. What was the distribution of nuts before they all had the same?
In the end, everyone had the same, i.e., 40 nuts each (120 : 3). Before that, Kuba gave Ondra nuts, doubling his amount. Before this exchange, Ondra had half of the final amount, so 20 nuts were transferred: Ondra had 20 and Kuba had 60 nuts (Matěj remained unchanged).

By similar reasoning, we can determine all the exchanges of nuts step by step from the end, and thus find the original numbers of nuts:

|  | Ondra | Matěj | Kuba |
| :--- | :---: | :---: | :---: |
| originally | 55 | 35 | 30 |
| Ondra to Matěj | 20 | 70 | 30 |
| Matěj to Kuba | 20 | 40 | 60 |
| Kuba to Ondra | 40 | 40 | 40 |

Originally, Ondra had 55, Matěj had 35, and Kuba had 30 nuts.

Another solution. If we denote the original numbers of nuts as $x, y$, and $z$, then the entire transaction proceeded as follows:

|  | Ondra | Matěj | Kuba |
| :--- | :---: | :---: | :---: |
| originally | $x$ | $y$ | $z$ |
| Ondra to Matěj | $x-y$ | $2 y$ | $z$ |
| Matěj to Kuba | $x-y$ | $2 y-z$ | $2 z$ |
| Kuba to Ondra | $2(x-y)$ | $2 y-z$ | $2 z-(x-y)$ |

According to the problem, the following equalities hold:

$$
2(x-y)=2 y-z=2 z-(x-y)=40 \text {. }
$$

The equality $2(x-y)=40$ is equivalent to $x-y=20$.

Substituting into the equality $2 z-(x-y)=40$, we get $2 z=60$, so $z=30$.

Substituting into the equality $2 y-z=40$, we get $2 y=70$, so $y=35$.

Finally, from the equality $x-y=20$, we get $x=55$.

Originally, Ondra had 55, Matěj had 35, and Kuba had 30 nuts.