provide both o(n*n) and o(n)