Added a space efficient solution for p13