Intro: As far as I could search, this question wasn\'t asked in SO yet.
This is an interview question.
I am not even specifically looking for a code sol
edit: you're right, I completely ignored the adjacency constraint. luckily I've thought of a solution. The algorithm goes like this:
(O(n)) (O(n)) O(1) - just an index check) O(n))