improving 2_1 solution
O(n^2) solution with no buffer