code for solution2