Yandex Interview Question

Leetcode : easy and medium