add O(n^2) solution without auxiliary array