new algo for problem 1