Should the question remain open on both sites, in this specific case? If not, should it be closed here on CS.SE?
Potentially secondary factors that may or may not be relevant:
We have made substantial revisions to the question here on CS.SE which, in my opinion, make our copy a better question (or, at least, better for CS.SE).
Among the answers received so far, the answers here on CS.SE seem better than the answers on Stack Overflow, in my opinion.
The problem statement, as quoted, claims this can be solved in $O(n)$ time. There has been doubt expressed about whether the problem can be solved in $O(n)$ time. It's not clear whether that has any relevance to this discussion, though.
For reference, cross-posting on multiple SE sites is generally not allowed: Is cross-posting a question on multiple Stack Exchange sites permitted if the question is on-topic for each site?. However, while this policy is a helpful guideline in general, no policy is absolute, so it's worthwhile to consider the specifics.
The poster not responded yet to requests to provide attribution or a citation to the original source of this question. It is apparently a challenge at university, according to a current comment here on CS.SE. (For our reference, here are our guidelines on referencing.)