Implement a simpler O(n) solution to this problem.