IBM interview question

Hacker Rank