My solution for problem on hackerrank.com