Solution to second problem