Solution to hackerearth problem 1