Hacker Rank problem solution