Domanda di colloquio di IBM

Magic Carpet question that was easy Leetcode question.