Solution of hackerrank problem