adding constant time space saving algorithm