IBM Interview Question

Leetcode medium question, I did not see hard question