Yandex Interview Question

2 tasks looks like leetcode easy-medium.