using constant space for problem 3