How did you came up with the idea that marking visited node negative will work ? It would be great if you could provide a reason/ reference for the same.
gandharjoshi
I can solve this O(n) speed like two sum but it requires O(n) space as well. I am not entirely sure how one can do this in O(1) but I probably need to practice in order to get my skill up to that level.
monkeytrollhunter
what is font being used here in writing code ? Is it default font of your browser?
sunilpulla
Your set solution is not O(n). Set insertion is log(n) and inserting n elements in the set is nlog(n).