Pseudo solved o(n) time complexity/efficency problem