My solution for hackerrank.com algorithms challenge