added a constant space solution