Add solution taking O(1) space.