an accepted O(n*log(n)) solution, but still need a O(n) solution