YANDEX Interview Question

One distance apart Leetcode 161