IBM interview question

Leetcode medium question, I did not see hard question