with O 1 space complex